System.Net.WebException:操作已在System.Net.HttpWebRequest.GetResponse超时

时间:2014-07-05 17:49:58

标签: c# httpwebrequest webrequest httpwebresponse webresponse

我正在尝试浏览某些网页,但有时我收到“操作已超时”错误..有时它会工作几天没有任何错误.. 收到错误后,如果我尝试从WebBrowser导航,它会毫无问题地导航,但如果我想再次使用webrequest导航,我会再次收到此错误...

我已经尝试将ReadWriteTimeout和Timeout属性设置为2-3分钟,直到无限......仍然没有运气:(

这是我的代码:

static void setHeaders(ref WebRequest reqPOST, string sUrl)
{
reqPOST = System.Net.WebRequest.Create(sUrl);
reqPOST.Method = "GET";
((HttpWebRequest)reqPOST).UserAgent = "dummie useragent";
((HttpWebRequest)reqPOST).Accept = "*/*";
((HttpWebRequest)reqPOST).KeepAlive = true;
((HttpWebRequest)reqPOST).Timeout = Timeout.Infinite;
reqPOST.Headers.Add("Accept-Language", "en-US");
reqPOST.Headers.Add("Accept-Encoding", "gzip, deflate");
}

static void getResponse(ref WebRequest reqPOST, ref WebResponse result, ref StreamReader reader, ref string responseFromServer)
{
result = reqPOST.GetResponse();
if (result.Headers.Get("Content-Encoding") != null && result.Headers.Get("Content-Encoding").ToLower() == "gzip")
    reader = new System.IO.StreamReader(new GZipStream(result.GetResponseStream(), CompressionMode.Decompress));
else
    reader = new System.IO.StreamReader(result.GetResponseStream());
responseFromServer = reader.ReadToEnd();
}
setHeaders(ref reqPOST, "somewebsite");
getResponse(ref reqPOST, ref result, ref reader, ref responseFromServer);
reader.Close();
result.Close();

请问任何想法?谢谢。

1 个答案:

答案 0 :(得分:0)

你应该尝试添加

reqPOST.Timeout = 1000000; // Increase timeout