【发布时间】:2014-01-08 14:11:31
【问题描述】:
我正在尝试从网站获取简单的 gzip 编码 html 响应,但它一直超时,以下是我的代码:
HttpWebRequest httpClient = (HttpWebRequest)WebRequest.Create(url);
httpClient.Method = "GET";
httpClient.Accept = "text/html, application/xhtml+xml, */*";
httpClient.Headers.Add("Accept-Encoding: gzip, deflate");
httpClient.Headers.Add("Accept-Language: en-US");
httpClient.Headers.Add("DNT: 1");
httpClient.ProtocolVersion = HttpVersion.Version10;
httpClient.KeepAlive = true;
httpClient.Timeout = System.Threading.Timeout.Infinite;
httpClient.CookieContainer = cookieJar;
String responseAsText;
using (HttpWebResponse response = (HttpWebResponse)httpClient.GetResponse())
{
System.IO.StreamReader sr;
if (response.ContentEncoding.Equals("gzip"))
{
sr = new StreamReader(new GZipStream(response.GetResponseStream(), CompressionMode.Decompress));
}
else
{
sr = new System.IO.StreamReader(response.GetResponseStream());
}
responseAsText = sr.ReadToEnd();
}
我要访问的网址是“https client.schwab.com/Login/SignOn/CustomerCenterLogin.aspx”
这在浏览器中工作得很好,使用 Fiddler 我查看了浏览器的请求标头,因为它的 Transfer-Encoding: chunked,我使用了 HttpVersion10
我也尝试设置httpClient.Timeout = System.Threading.Timeout.Infinite,但它永远不会得到响应,但是在浏览器中响应会在几秒钟内得到。
请有人帮助我实现这一目标。
【问题讨论】:
标签: c# httpwebrequest httpwebresponse