C# HTTP ReadWriteTimeout 问题
我正在使用 HttpWebRequest 类,并对 Timeout/ReadWriteTimeout 成员感到困惑。问题是 ReadWriteTimeout 有时会被忽略,然后我的应用程序在我的 GetResponse()
调用上恰好挂起 5 分钟。有时它只是抛出一个 WebException,有时它会挂起 5 分钟,这似乎是默认值。我什至在调用 GetResponse()
之前检查了 ReadWriteTimeout 值,它始终是正确的值,我将其设置为 10000。
var getPage = WebRequest.Create(url) as HttpWebRequest;
getPage.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
getPage.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.19) Gecko/20110707 Firefox/3.6.19";
getPage.ProtocolVersion = HttpVersion.Version11;
getPage.Method = "GET";
getPage.CookieContainer = foo;
getPage.Proxy = proxyHTTP;
getPage.Timeout = 10000;
getPage.ReadWriteTimeout = 10000;
// 在另一个函数中,我将 getPage 传递给
Console.WriteLine("Timeout: {0} / ReadWriteTimeout{1}", page.Timeout, page.ReadWriteTimeout);
var pageResponse = (HttpWebResponse)page.GetResponse();
Console.WriteLine("It reaches this line after 5 minutes");
if (pageResponse.StatusCode == HttpStatusCode.OK)
{
// read and close it afterwards
}
我,我使用的是 HTTP 代理。奇怪的是,该网址在浏览器中加载正常。我将不胜感激任何意见。
// 编辑如下
namespace Proxy
{
class Program
{
static void Main(string[] args)
{
var htmlResponse = new StringBuilder();
var RequestPage = BuildHttpRequest("https://twitter.com/signup");
GetHttpResponse(RequestPage, htmlResponse);
}
public static HttpWebRequest BuildHttpRequest(string url)
{
try
{
var getPage = (HttpWebRequest)WebRequest.Create(url);
WebProxy proxyHTTP = new WebProxy("201.38.194.50", 3128);
getPage.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
getPage.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.19) Gecko/20110707 Firefox/3.6.19";
getPage.ProtocolVersion = HttpVersion.Version11;
getPage.Method = "GET";
getPage.Proxy = proxyHTTP;
getPage.Timeout = 10000;
getPage.ReadWriteTimeout = 10000;
return getPage;
}
catch (WebException ex)
{
Console.WriteLine(ex.ToString());
}
return null;
}
public static bool GetHttpResponse(HttpWebRequest page, StringBuilder html)
{
html.Clear();
try
{
Console.WriteLine("A");
var pageResponse = (HttpWebResponse)page.GetResponse();
Console.WriteLine("5 minutes!");
if (pageResponse.StatusCode == HttpStatusCode.OK)
{
var reader = new StreamReader(pageResponse.GetResponseStream());
html.Append(reader.ReadToEnd());
pageResponse.Close();
reader.Close();
return true;
}
Console.WriteLine(pageResponse.StatusCode.ToString());
pageResponse.Close();
return false;
}
catch (WebException ex)
{
Console.WriteLine(ex.ToString());
}
return false;
}
}
}
A first chance exception of type 'System.Net.WebException' occurred in System.dll
System.Net.WebException: The operation has timed out
at System.Net.HttpWebRequest.GetResponse()
I'm playing around with the HttpWebRequest
class and got confused about the Timeout/ReadWriteTimeout members. The issues is that the ReadWriteTimeout gets ignored sometimes and then my application hangs exactly 5 minutes on my GetResponse()
call. Sometimes it simply throws a WebException
and sometimes it hangs for 5 minutes, which seems to be the default value. I even check the ReadWriteTimeout value before calling GetResponse()
and it's always the correct value, which I set to 10000.
var getPage = WebRequest.Create(url) as HttpWebRequest;
getPage.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
getPage.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.19) Gecko/20110707 Firefox/3.6.19";
getPage.ProtocolVersion = HttpVersion.Version11;
getPage.Method = "GET";
getPage.CookieContainer = foo;
getPage.Proxy = proxyHTTP;
getPage.Timeout = 10000;
getPage.ReadWriteTimeout = 10000;
// in another function where I pass the getPage to
Console.WriteLine("Timeout: {0} / ReadWriteTimeout{1}", page.Timeout, page.ReadWriteTimeout);
var pageResponse = (HttpWebResponse)page.GetResponse();
Console.WriteLine("It reaches this line after 5 minutes");
if (pageResponse.StatusCode == HttpStatusCode.OK)
{
// read and close it afterwards
}
I am using a HTTP Proxy. The weird part is that the url loads fine in the browser. I'd be thankful for any input.
// EDIT BELOW
namespace Proxy
{
class Program
{
static void Main(string[] args)
{
var htmlResponse = new StringBuilder();
var RequestPage = BuildHttpRequest("https://twitter.com/signup");
GetHttpResponse(RequestPage, htmlResponse);
}
public static HttpWebRequest BuildHttpRequest(string url)
{
try
{
var getPage = (HttpWebRequest)WebRequest.Create(url);
WebProxy proxyHTTP = new WebProxy("201.38.194.50", 3128);
getPage.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
getPage.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.19) Gecko/20110707 Firefox/3.6.19";
getPage.ProtocolVersion = HttpVersion.Version11;
getPage.Method = "GET";
getPage.Proxy = proxyHTTP;
getPage.Timeout = 10000;
getPage.ReadWriteTimeout = 10000;
return getPage;
}
catch (WebException ex)
{
Console.WriteLine(ex.ToString());
}
return null;
}
public static bool GetHttpResponse(HttpWebRequest page, StringBuilder html)
{
html.Clear();
try
{
Console.WriteLine("A");
var pageResponse = (HttpWebResponse)page.GetResponse();
Console.WriteLine("5 minutes!");
if (pageResponse.StatusCode == HttpStatusCode.OK)
{
var reader = new StreamReader(pageResponse.GetResponseStream());
html.Append(reader.ReadToEnd());
pageResponse.Close();
reader.Close();
return true;
}
Console.WriteLine(pageResponse.StatusCode.ToString());
pageResponse.Close();
return false;
}
catch (WebException ex)
{
Console.WriteLine(ex.ToString());
}
return false;
}
}
}
A first chance exception of type 'System.Net.WebException' occurred in System.dll
System.Net.WebException: The operation has timed out
at System.Net.HttpWebRequest.GetResponse()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你能试试这个吗
Can you try this