Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adjusting HttpWebRequest Connection Timeout in C#

I believe after lengthy research and searching, I have discovered that what I want to do is probably better served by setting up an asynchronous connection and terminating it after the desired timeout... But I will go ahead and ask anyway!

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. 

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.

So the question is: "Is there a simple way to specify/decrease a connection timeout for a WebRequest or HttpWebRequest?"

like image 274
JYelton Avatar asked Sep 30 '09 22:09

JYelton


People also ask

How do I set HttpWebRequest timeout?

Quick snippet of code: HttpWebRequest webReq = (HttpWebRequest)HttpWebRequest. Create(url); webReq. Timeout = 5000; HttpWebResponse response = (HttpWebResponse)webReq.

What is the default timeout for HttpWebRequest?

The default value is 100,000 milliseconds (100 seconds).

What is HttpWebRequest C#?

The HttpWebRequest class provides support for the properties and methods defined in WebRequest and for additional properties and methods that enable the user to interact directly with servers using HTTP.


1 Answers

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.

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.

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.

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.

like image 133
Remus Rusanu Avatar answered Sep 23 '22 19:09

Remus Rusanu