我想從一個網站簡單的gzip編碼的HTML響應,並不斷變得超時,下面是我的代碼:HttpWebRequest.GetResponse操作超時
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」
這工作完全正常的瀏覽器,使用招我認爲瀏覽器的請求頭和自Transfer-Encoding: chunked
,我已經使用HttpVersion10
我也tr ied設置httpClient.Timeout = System.Threading.Timeout.Infinite
,但它永遠不會收到響應,但是在瀏覽器中響應會在幾秒鐘內完成。
請有人幫助我實現這一目標。
你的意思是添加User-Agent頭? –
httpClient.DefaultRequestHeaders.TryAddWithoutValidation(「Accept」,「text/html,application/xhtml + xml,application/xml」); httpClient.DefaultRequestHeaders.TryAddWithoutValidation(「Accept-Encoding」,「gzip,deflate」);httpClient.DefaultRequestHeaders.TryAddWithoutValidation(「User-Agent」,「Mozilla/5.0(Windows NT 6.2; WOW64; rv:19.0)Gecko/20100101 Firefox/19.0」); httpClient.DefaultRequestHeaders.TryAddWithoutValidation(「Accept-Charset」,「ISO-8859-1」); – realnero
試試這些頭文件 – realnero