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
thein
source share