0

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应用程序的日志):

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
4

2 回答 2

0

我需要做的就是禁止自动请求重定向:

request.AllowAutoRedirect = false;
于 2012-11-24T09:02:04.663 回答
0

去除:

_request.Content.Headers.ContentEncoding.Add("gzip");
_request.Content.Headers.ContentEncoding.Add("deflate");

因为您没有发送压缩内容

另外,我建议您使用 WireShak 查看与您的代码一起实际发送的内容 - 因为我确信HttpRequestMessage已经附加了您需要的大部分标头。

于 2012-11-09T23:09:21.773 回答