[英]HttpWebRequest.AllowAutoRedirect=false can cause timeout?
I need to test around 300 URLs to verify if they lead to actual pages or redirect to some other page. 我需要测试大约300个URL,以验证它们是否指向实际页面或重定向到其他页面。 I wrote a simple application in .NET 2.0 to check it, using HttpWebRequest.
我使用HttpWebRequest在.NET 2.0中编写了一个简单的应用程序进行检查。 Here's the code snippet:
这是代码片段:
System.Net.HttpWebRequest wr = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create( url );
System.Net.HttpWebResponse resp = (System.Net.HttpWebResponse)wr.GetResponse();
code = resp.StatusDescription;
Code ran fast and wrote to file that all my urls return status 200 OK. 代码运行很快,并写入文件,我所有的URL返回状态200 OK。 Then I realized that by default GetResponse() follows redirects.
然后我意识到默认情况下,GetResponse()遵循重定向。 Silly me!
傻我! So I added one line to make it work properly:
因此,我添加了一行以使其正常工作:
System.Net.HttpWebRequest wr = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create( url );
wr.AllowAutoRedirect = false;
System.Net.HttpWebResponse resp = (System.Net.HttpWebResponse)wr.GetResponse();
code = resp.StatusDescription;
I ran the program again and waited... waited... waited... It turned out that for each url I was getting a System.Net.WebException "The operation has timed out". 我再次运行该程序,然后等待...等待...等待...原来,对于每个URL,我都得到一个System.Net.WebException“操作已超时”。 Surprised, I checked the URL manually - works fine... I commented out AllowAutoRedirect = false line - and it works fine again.
感到惊讶,我手动检查了URL-正常工作...我注释掉AllowAutoRedirect = false行-再次正常工作。 Uncommented this line - timeout.
未注释此行-超时。 Any ideas what might cause this problem and how to work around?
有什么想法可能导致此问题以及如何解决?
Often timeouts are due to web responses not being disposed. 通常超时是由于未处理Web响应。 You should have a
using
statement for your HttpWebResponse
: 您应该为
HttpWebResponse
using
语句:
using (HttpWebResponse resp = (HttpWebResponse)wr.GetResponse())
{
code = resp.StatusDescription;
// ...
}
We'd need to do more analysis to predict whether that's definitely the problem... or you could just try it :) 我们需要做更多的分析,以预测这绝对是问题所在……或者您可以尝试一下:)
The reason is that .NET has a connection pool, and if you don't close the response, the connection isn't returned to the pool (at least until the GC finalizes the response). 原因是.NET有一个连接池,如果您不关闭响应,则连接不会返回到池中(至少在GC完成响应之前)。 That leads to a hang while the request is waiting for a connection.
当请求正在等待连接时,这导致挂起。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.