我试图从网站上获取简单的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我查看了浏览器的Request标头,因为Transfer-Encoding: chunked
,我使用了HttpVersion10
我也尝试过设置httpClient.Timeout = System.Threading.Timeout.Infinite
,但它永远不会回复,但是在浏览器中,响应会在几秒钟内完成。
请有人帮助我实现这一目标。
答案 0 :(得分:1)
可能您可以尝试设置Agent属性,因此它不会将您识别为机器人。
答案 1 :(得分:0)
我认为Nero已经回答了你的问题..
尝试在代码中添加这些行..
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0";