简体   繁体   中英

HttpWebRequest.GetResponse Operation has timed out

I'm trying to get simple gzip encoded html response from a website and it keeps getting time out, following is my code:

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();
 }

The url I'm trying to hit is "https client.schwab.com/Login/SignOn/CustomerCenterLogin.aspx"

This works perfectly fine in the Browser, using Fiddler I viewed the browser's Request header and since its Transfer-Encoding: chunked , I have used HttpVersion10

I have also tried setting httpClient.Timeout = System.Threading.Timeout.Infinite , but it never gets back with a response, however in browser the response gets in few seconds.

Please someone help me in achieving this.

可能您可以尝试设置代理属性,这样它就不会将您识别为漫游器。

I think Nero has answered your question ..

Try adding these Lines in your code..

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM