使用 WebClient 获取 HTTP 中的奇怪超时 WebException
我有一个多线程应用程序(在 C# 中),它对 uri 执行多个 HTTP GET
操作。 突然,在数千个请求之后,我收到超时 webException,但如果我使用浏览器进行测试,目标服务器就可以了。 代码是:
public static string Get(string uri)
{
string responseData = string.Empty;
using (WebClient wc = new WebClient())
{
responseData = wc.DownloadString(uri);
}
return responseData;
}
我认为这是处理连接问题或类似错误。 任何身体都有同样的问题吗?
提前谢谢,
PS 我也使用过 HttpWebResponse
。 但我得到同样的错误。
PS操作系统是Windows 2003服务器。 所以我认为不是连接限制。
我也尝试过这段代码,但得到了同样的错误
public static string Get2(string uri)
{
string responseData = string.Empty;
WebRequest request = WebRequest.Create(uri) as HttpWebRequest;
request.Method = "GET";
request.Timeout = 35000;
using(HttpWebResponse response = (HttpWebResponse)request.GetResponse() as HttpWebResponse)
{
using(Stream dataStream = response.GetResponseStream ())
{
using(StreamReader reader = new StreamReader (dataStream))
{
responseData = reader.ReadToEnd();
}
}
}
return responseData;
}
I have a multi-thread application (in c#) that it does multiples HTTP GET
to uri. Suddenly after of thousand of requests, i get timeout webException but destination server is OK if i test using browser. The code is:
public static string Get(string uri)
{
string responseData = string.Empty;
using (WebClient wc = new WebClient())
{
responseData = wc.DownloadString(uri);
}
return responseData;
}
I think it is dispose connection issue or similar error. Any body has same problem?
Thx in advance,
PS I have used HttpWebResponse
too. But i get same error.
PS OS is Windows 2003 server. So i think is not connection limits.
I have try with this code too and i get same error
public static string Get2(string uri)
{
string responseData = string.Empty;
WebRequest request = WebRequest.Create(uri) as HttpWebRequest;
request.Method = "GET";
request.Timeout = 35000;
using(HttpWebResponse response = (HttpWebResponse)request.GetResponse() as HttpWebResponse)
{
using(Stream dataStream = response.GetResponseStream ())
{
using(StreamReader reader = new StreamReader (dataStream))
{
responseData = reader.ReadToEnd();
}
}
}
return responseData;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
快速问题
查看是否有丢包的报告
从您的客户端计算机。
您是否想过,服务器可能会耗尽处理您的请求的所有资源,然后简单地自行重新启动。 这就是为什么当您执行查询时会出现超时,但当您执行浏览器 ping 时,它会备份。 只是一个想法
既然您提到“数千个请求”,我更倾向于认为服务器上的资源紧缩而不是连接问题。
Quick question(s)
see if any packet loss is reported
from your client machine.
Have you thought of maybe the server exhausts all its resources handling your requests and just plain and simple restarts itself. Thats why you get a timeout when you are doing a query but by the time you do the browser ping, it is back up. Just a thought
Since, you mention "thousands of requests", I am more inclined to think a resource crunch on the server rather than a connectivity issue.
这可能是由多种原因造成的,包括:
短暂的连接中断
您的应用程序和
远程 Web 服务器
由于负载,远程服务器可能无法及时响应
我建议在请求上设置更长的超时,但您需要使用
WebRequest
因为无法在WebClient
上设置超时。This could be caused by any number of reasons including:
Brief transient loss of connectivity
between your application and the
remote web server
The remote server is possibly not responding in time due to load
I suggest setting a longer timeout on the request but you'd need to use
WebRequest
instead because there is no way to set a timeout onWebClient
.我自己遇到过这个问题,我发现有效的办法是在该线程上短暂的 Wait() 5 秒后尝试多次获取请求,如果它不起作用,最好让服务器单独运行一段时间。 :-)
Having faced this myself what I found to work is to try your get request a few more times after a short Wait() on that thread for say 5 seconds and if it doesn't work it is best to leave the server alone for some time. :-)
一种可能性是端口耗尽。
您将针对每个新请求创建一个新的 WebClient。 这意味着它将在每个请求上获取一个新的 TCP 连接和端口。 在 Windows 机器上,端口仅在 16 位范围内,因此 0 - 65535 并且操作系统对此范围施加了进一步的限制。
如果您在 Windows 计算机上执行
NETSTAT -on
a,您将看到连接及其分配的端口列表。问题是端口只有在进入 TIME_WAIT 状态 > 30 秒后才会返回。 这意味着,如果您每 30 秒以内发出一个新请求,您最终将耗尽端口,并且会发生不好的事情。
我建议创建一个 WebClient 并重用它。 您不仅可以避免上述问题,而且性能也更高。
One possibility is Port Exhaustion.
You are creating a new WebClient on each new request. This means it will grab a new TCP connection and port on every request. On a windows machine ports are only in the 16 bit range so 0 - 65535 and the OS imposes further resictions on this range.
If you do a
NETSTAT -on
a on your Windows machine, you'll see a list of connections and their assigned ports.The issue is that ports are only returned after >30s as they enter a
TIME_WAIT
state. This means if you making a new request every <30s you will eventually run out of ports and bad things will happen.I recommend creating a single WebClient and reusing it. Not only can you avoid the above described issue but it is also more performant.