简体   繁体   English

HttpWebRequest.GetResponse操作已超时

[英]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: 我正在尝试从网站获取简单的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();
 }

The url I'm trying to hit is "https client.schwab.com/Login/SignOn/CustomerCenterLogin.aspx" 我要输入的网址是“ 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 这在浏览器中工作得很好,使用Fiddler,我查看了浏览器的Request标头,并且由于它的Transfer-Encoding: chunked HttpVersion10 ,我使用了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. 我也尝试过设置httpClient.Timeout = System.Threading.Timeout.Infinite ,但是它永远不会返回响应,但是在浏览器中响应会在几秒钟内得到。

Please someone help me in achieving this. 请有人帮助我实现这一目标。

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

I think Nero has answered your question .. 我认为Nero回答了您的问题..

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";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在获取WebException:“操作已超时”立即在HttpWebRequest.GetResponse()上 - I am getting WebException: “The operation has timed out” immediately on HttpWebRequest.GetResponse() HttpWebRequest.GetResponse() 不断超时 - HttpWebRequest.GetResponse() keeps getting timed out 在httpWebRequest.GetResponse()方法中操作超时异常关闭连接 - Does Operation Timed out exception in httpWebRequest.GetResponse() method close the connection HttpWebRequest .GetResponse 抛出 WebException '操作已超时' - HttpWebRequest .GetResponse throws WebException 'The operation has timed out' 向主机发送大量请求时 System.Net.HttpWebRequest.GetResponse() 的操作超时 - The operation has timed out at System.Net.HttpWebRequest.GetResponse() while sending large number of requests to a host HttpWebRequest - 操作已超时 - HttpWebRequest - The operation has timed out System.Net.WebException:操作在 System.Net.HttpWebRequest.GetResponse 超时 - System.Net.WebException: The operation has timed out at System.Net.HttpWebRequest.GetResponse 从亚马逊 s3 读取图像和视频时,在 System.Net.HttpWebRequest.GetResponse() 处获取 WebException“操作已超时” - Getting WebException “The operation has timed out” at System.Net.HttpWebRequest.GetResponse() when reading images and video from amazon s3 HttpWebRequest请求被中止:操作已超时 - HttpWebRequest The request was aborted: The operation has timed out HttpWebRequest.GetResponse 无论如何总是超时 - HttpWebRequest.GetResponse always times out no matter what
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM