C#HTTP读写超时问题
我在玩C#HTTP读写超时问题,c#,timeout,httpwebrequest,C#,Timeout,Httpwebrequest,我在玩HttpWebRequest类,对Timeout/ReadWriteTimeout成员感到困惑。问题是ReadWriteTimeout有时会被忽略,然后我的应用程序在调用GetResponse()时正好挂起5分钟。有时它只是抛出一个WebException,有时它会挂起5分钟,这似乎是默认值。我甚至在调用GetResponse()之前检查ReadWriteTimeout值,它总是正确的值,我将其设置为10000 //在另一个函数中,我将getPage传递给 Console.WriteLin
HttpWebRequest
类,对Timeout/ReadWriteTimeout成员感到困惑。问题是ReadWriteTimeout有时会被忽略,然后我的应用程序在调用GetResponse()
时正好挂起5分钟。有时它只是抛出一个WebException
,有时它会挂起5分钟,这似乎是默认值。我甚至在调用GetResponse()
之前检查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代理。奇怪的是url在浏览器中加载得很好。我非常感谢您的任何意见
//编辑如下
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;
}
}
}
你能试试这个吗
var request = (HttpWebRequest)WebRequest.Create(url);
using (var pageResponse = request.GetResponse())
{
// Do stuff with `response` here
}
// drop the as HttpWebRequest;
// The response however is, these will eventually be reclaimed by the GC
// but you'll run into problems similar to deadlocks if you don't dispose them yourself
// when you have many of them
你能粘贴得到的确切错误吗…?没有错误,它只是挂起5分钟,这是ReadWriteTimeout的默认值。然后抛出WebException.question是默认设置的以秒或毫秒为单位的时间。。也许你需要把时间调整下来,看看..这是毫秒,调整时间不会改变任何事情,因为它只是忽略了时间。其他代理在10秒后超时正常,但这一个没有。好的,我现在明白了。。好的,我以前有相同的代码。我也想过,但这是我的第一个请求,在此之前没有其他请求。还是谢谢你!你的代码中一定有一些东西,无论是铸造还是扔掉了我们缺少的东西。我举了一个小例子,如果你能阅读编辑,我会很高兴。当然,我可以看看它,看看我是否看到任何奇怪或不合适的东西。我想你也看不出任何东西了吧?真奇怪。
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()
var request = (HttpWebRequest)WebRequest.Create(url);
using (var pageResponse = request.GetResponse())
{
// Do stuff with `response` here
}
// drop the as HttpWebRequest;
// The response however is, these will eventually be reclaimed by the GC
// but you'll run into problems similar to deadlocks if you don't dispose them yourself
// when you have many of them