2014-01-08 165 views
0

我想從一個網站簡單的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,但它永遠不會收到響應,但是在瀏覽器中響應會在幾秒鐘內完成。

請有人幫助我實現這一目標。

回答

1

可能您可以嘗試設置Agent屬性,因此它不會將您識別爲bot。

+0

你的意思是添加User-Agent頭? –

+0

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

+0

試試這些頭文件 – realnero

0

我認爲尼祿已經回答了你的問題..

嘗試增加你的這幾行代碼..

request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0";