执行以下代码时,我遇到The underlying connection was closed: The connection was closed unexpectedly
错误(Windows 8,.net 4.5):
link = @"http://login.rutracker.org/forum/index.php";
_request = new HttpRequestMessage(HttpMethod.Post, link);
_request.Headers.Add("User-Agent", "Chrome/22.0.1229.94");
_request.Headers.Add("Accept", "text/html");
var content = new Dictionary<string, string>();
content.Add("login_username", name);
content.Add("login_password", "pass");
_request.Content = new FormUrlEncodedContent(content);
_request.Content.Headers.ContentEncoding.Add("gzip");
_request.Content.Headers.ContentEncoding.Add("deflate");
_response = await _client.GetAsync(_request.RequestUri);
return await _response.Content.ReadAsByteArrayAsync();
我的目标是提出与以下相同的请求(从Fiddler
app登录):
POST /forum/login.php HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Referer: http://rutracker.org/forum/index.php
Accept-Language: en-US
User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
Content-Length: 71
DNT: 1
Host: login.rutracker.org
Pragma: no-cache
Cookie: spylog_test=1
login_username=myusername&login_password=mypass&login=%C2%F5%EE%E4
答案 0 :(得分:0)
删除:
_request.Content.Headers.ContentEncoding.Add("gzip");
_request.Content.Headers.ContentEncoding.Add("deflate");
因为您没有发送gzip压缩内容
此外,我建议您使用WireShak查看实际使用您的代码发送的内容 - 因为我确信HttpRequestMessage
已经附加了您需要的大多数标头。
答案 1 :(得分:0)
我需要做的就是禁止自动请求重定向:
request.AllowAutoRedirect = false;