justcoding121 / titanium-web-proxy

A cross-platform asynchronous HTTP(S) proxy server in C#.
MIT License
1.92k stars 607 forks source link

You must write ContentLength bytes to the request stream before calling [Begin]GetResponse #170

Closed looselive closed 7 years ago

looselive commented 7 years ago

When i post some data to a url get this err You must write ContentLength bytes to the request stream before calling [Begin]GetResponse.

looselive commented 7 years ago

Not use this peoxy server it's normal,plase help me!

looselive commented 7 years ago

My codes:

byte[] bytes = Encoding.UTF8.GetBytes("uid=&langx=zh-cn&mac=&ver=&JE=&username=" + this.Username + "&password=" + this.Password + "&checkbox=on");
var request = CreateRequest("http://xxx/login.php");
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = (long)bytes.Length;
request.Method = "POST";
request.Referer = "http://xxx/";

var stream = await request.GetRequestStreamAsync();
await stream.WriteAsync(bytes, 0, bytes.Length);

var response = await request.GetResponseAsync();
stream = response.GetResponseStream();
var streamReader = new StreamReader(stream, Encoding.UTF8);
string responseHTML = await streamReader.ReadToEndAsync();
streamReader.Dispose();
stream.Close();
response.Dispose();
request.Abort();
honfika commented 7 years ago

Put this:

            var stream = await request.GetRequestStreamAsync();
            await stream.WriteAsync(bytes, 0, bytes.Length);

to a using block:

            using (var stream = await request.GetRequestStreamAsync()) {
                await stream.WriteAsync(bytes, 0, bytes.Length);
            }

Or maybe a Flush(Async) is enough, too. But "using" is better. And you should use "using" in the response stream, too.