2012-11-09 115 views
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 

回答

0

所有我需要做的是禁止自然而然请求重定向:

request.AllowAutoRedirect = false; 
0

卸下:

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

因为你没有发送gzip内容

此外,我会建议你看看它实际上与你的代码与WireShak发送 - 因为我相信HttpRequestMessage已经附加了大部分你需要的标题。

+0

我试过发送/不发送头的各种组合,包括删除这些头,它仍然无法正常工作。 –

+0

你可以通过使用你的代码发布你使用Fiddler获得的输出吗? – thedayofcondor

相关问题