调整HttpWebRequest的连接超时在C# [英] Adjusting HttpWebRequest Connection Timeout in C#

查看:435
本文介绍了调整HttpWebRequest的连接超时在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信长期的研究和搜索之后,我发现,我想要做的就是通过建立异步连接和所需超时后终止它可能提供更好的服务......但我会继续和问问反正!

code的快速片段:

Quick snippet of code:

HttpWebRequest webReq = (HttpWebRequest)HttpWebRequest.Create(url);
webReq.Timeout = 5000;
HttpWebResponse response = (HttpWebResponse)webReq.GetResponse(); 
// this takes ~20+ sec on servers that aren't on the proper port, etc.

我有一个的HttpWebRequest 方法是在多线程应用程序,其中我连接到大量公司的网络服务器。在服务器没有响应, HttpWebRequest.GetResponse()正在约20秒超时,即使我指定的只有5秒的超时的情况。通过在服务器上以固定的间隔获得的利益,我想跳过那些服用时间超过5秒的连接。

I have an HttpWebRequest method that is in a multi-threaded application, in which I am connecting to a large number of company web servers. In cases where the server is not responding, the HttpWebRequest.GetResponse() is taking about 20 seconds to time out, even though I have specified a timeout of only 5 seconds. In the interest of getting through the servers on a regular interval, I want to skip those taking longer than 5 seconds to connect to.

所以,问题是:?是否有指定/减小了的WebRequest或HttpWebRequest的一个连接超时的简单方法

推荐答案

我的认为的,问题是,的WebRequest 测量只有时间之后该请求实际上是由。如果您提交多个请求到同一个地址,然后在 ServicePointManager 将扼杀你的请求,实际上只提交多并发连接作为相应的值<一个href=\"http://msdn.microsoft.com/en-us/library/system.net.servicepoint.connectionlimit.aspx\"><$c$c>ServicePoint.ConnectionLimit在默认情况下会从值<一个href=\"http://msdn.microsoft.com/en-us/library/system.net.servicepointmanager.defaultconnectionlimit.aspx\"><$c$c>ServicePointManager.DefaultConnectionLimit.应用CLR主机将其设置为2,ASP主机10.所以,如果你有一个多线程的应用程序提交多个请求到同一主机只有两个实际上放置在电线外,其余都排队。

I believe that the problem is that the WebRequest measures the time only after the request is actually made. If you submit multiple requests to the same address then the ServicePointManager will throttle your requests and only actually submit as many concurrent connections as the value of the corresponding ServicePoint.ConnectionLimit which by default gets the value from ServicePointManager.DefaultConnectionLimit. Application CLR host sets this to 2, ASP host to 10. So if you have a multithreaded application that submits multiple requests to the same host only two are actually placed on the wire, the rest are queued up.

我还没有研究这一个确凿的证据,这是否是真正发生的事情,而是一个类似的项目,我的东西是可怕的,直到我删除了的ServicePoint 限制。

I have not researched this to a conclusive evidence whether this is what really happens, but on a similar project I had things were horrible until I removed the ServicePoint limitation.

要考虑的另一个因素是DNS查找时间。同样,我相信没有确凿的证据支持,但我觉得的WebRequest 做的的计算在请求超时DNS查询的时间。 DNS查找时间显示为非常大的时间因素上的一些部署。

Another factor to consider is the DNS lookup time. Again, is my belief not backed by hard evidence, but I think the WebRequest does not count the DNS lookup time against the request timeout. DNS lookup time can show up as very big time factor on some deployments.

是的,你必须code周围的<一个您的应用程序href=\"http://msdn.microsoft.com/en-us/library/system.net.webrequest.begingetrequeststream.aspx\"><$c$c>WebRequest.BeginGetRequestStream (对于 POST s的内容)和<一个href=\"http://msdn.microsoft.com/en-us/library/system.net.webrequest.begingetresponse.aspx\"><$c$c>WebRequest.BeginGetResponse (对于 GET 取值的帖子 S)。同步调用不会缩放(我不会进入细节为什么,但我的的有确凿证据)。无论如何,的ServicePoint 问题是正交的:排队行为发生与异步调用了。

And yes, you must code your app around the WebRequest.BeginGetRequestStream (for POSTs with content) and WebRequest.BeginGetResponse (for GETs and POSTSs). Synchronous calls will not scale (I won't enter into details why, but that I do have hard evidence for). Anyway, the ServicePoint issue is orthogonal to this: the queueing behavior happens with async calls too.

这篇关于调整HttpWebRequest的连接超时在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆