Can't set the HttpWebRequest timeout to above 100 seconds while doing a POST?

I ran into a problem when the HttpWebRequest would not honor a timeout value above 100 seconds when doing a POST. However, if the request is a GET, a timeout value greater than 100 seconds is respected. A timeout exception was thrown by calling .GetResponse (). I set all the timeout values ​​that I could detect, but it seems that I am missing one, or there is an error in the structure.

This is a C # application targeted at the .NET Framework 3.5, built using Visual Studio 2008. The web server is IIS 6.0 with the connection timeout set to 120 seconds by default, support for keep-alives ... again GET requests respect the value The timeout that I specify is POST requests timeout if <= 100 seconds.

Here is my code:

int timeout = 200000; // 200 seconds HttpWebRequest proxyRequest = (HttpWebRequest)WebRequest.Create(serverUrl); proxyRequest.Accept = clientRequest.AcceptTypes.ToDelimitedString(", "); proxyRequest.Method = "POST" proxyRequest.UserAgent = clientRequest.UserAgent; proxyRequest.Timeout = timeout; proxyRequest.ReadWriteTimeout = timeout; proxyRequest.KeepAlive = false; proxyRequest.AllowAutoRedirect = false; proxyRequest.ServicePoint.Expect100Continue = false; proxyRequest.ServicePoint.MaxIdleTime = timeout; proxyRequest.ServicePoint.ConnectionLeaseTimeout = -1; try { // add post data request.ContentType = "application/x-www-form-urlencoded"; byte[] postData = Encoding.UTF8.GetBytes("somedata=7&moredata=asdf"); // set some post data request.ContentLength = postData.Length; using (Stream stream = request.GetRequestStream()) { stream.Write(postData, 0, postData.Length); stream.Close(); } // UPDATE // don't set Timeout here! It will be ignored // proxyRequest.Timeout = timeout; // Timeout exception thrown here if GetResponse doesn't return within 100 seconds // even though the Timeout value is set to 200 seconds. using (HttpWebResponse proxyResponse = (HttpWebResponse)proxyRequest.GetResponse()) { using (Stream stream = proxyResponse.GetResponseStream()) { using (StreamReader reader = new StreamReader(stream, Encoding.Default)) { string content = reader.ReadToEnd(); [other pointless code for this example] reader.Close(); } stream.Close(); } proxyResponse.Close(); } } finally { proxyRequest.Abort(); } 

When I set the timeout value to 5 seconds, I will get a timeout exception after 5 seconds, as expected. This proves that the value of Timeout is not completely ignored.

Anyone else run into this problem? Will this problem be used in the Async version of GetResponse? Any thoughts are welcome, I'm stuck on this a couple of days.

UPDATE

I can make POST respect the timeout value if I don't post any data (which is not very useful). However, as soon as I send any data at all, and ContentLength β†’ 0, the time elapses after 100 seconds. In addition, no proxies are involved.

UPDATE 2

Added POST data to an example and comment on where to NOT set the Timeout property

+8
source share
2 answers

I get it. This is an example of DRY coding coming back and biting me in the butt. The code above is a paraphrase of my real code, and as such, the code above will work fine.

The problem was setting the Timeout value after I already called proxyRequest.GetRequestStream () to add the POST data. Since I set the Timeout and ReadWriteTimeout properties, the shortest timeout was a win. In the case of a POST request, even though the framework allowed me to set the Timeout value AFTER calling GetRequestStream, it ignored any value set (and instead used 100 seconds by default, although when checking the Timeout property after setting it was shown that it so I expected). I want the setting of the Timeout property to work the same way as setting the ReadWriteTimeout property: if you try to set the ReadWriteTimeout property after calling GetRequestStream, it throws an exception. If Timeout did the same, that would save me ton of time. I should have caught this before, but I will talk about it with training experience.

So, the moral of this story: set all the timeout properties for your HttpWebRequest right when you create it.

+17
source

Do you have a web proxy between your client and server? Perhaps this is using the timeout itself.

I suggest you use Wireshark to find out what is happening at the network level - and in particular, something is happening on the network after 100 seconds.

+3
source

All Articles