February 1st, 2005

Timeouts on the HttpWebRequest (and thus SOAP proxies)

Buck Hodges
Director of Engineering

Last summer I wrote several posts on using HttpWebRequest and SOAP proxies.  Recently I was working on code that needed to handle requests that could take a really long time for the server to process.  The client would give up and close the network connection long before the request completed.

The code had tried to control that using the HttpWebRequest.Timeout property.  That helped, but it didn’t solve the problem.  Without changing the Timeout property, the client gave up after 100 seconds, which is the default value for that property as stated in the docs.  The Timeout property was set to infinite, but the client gave up after five minutes.  Below is the doc for the Timeout property.

Return Value

The number of milliseconds to wait before the request times out. The default is 100000 milliseconds (100 seconds).

Remarks

Timeout is the number of milliseconds that a synchronous request made with the GetResponse method waits for a response, and the GetRequestStream method waits for a stream. If the resource is not returned within the time-out period, the request throws a WebException with the Status property set to Timeout.

The Timeout property has no effect on asynchronous requests made with the BeginGetResponse or BeginGetRequestStream methods.

Caution In the case of asynchronous requests, it is the responsibility of the client application to implement its own timeout mechanism. Refer to the example in the BeginGetResponse method.

To specify the amount of time to wait before a read or write operation times out, use the ReadWriteTimeout property.

At the end of that, notice that they mention another timeout value, the HttpWebRequest.ReadWriteTimeout property.  Here’s the doc for that.

Return Value

The number of milliseconds before the writing or reading times out. Its default value is 300000 milliseconds (5 minutes).

Remarks

The ReadWriteTimeout is used when writing to the stream returned by GetRequestStream or reading from the stream returned by GetResponseStream.

Specifically, the ReadWriteTimeout property controls the time-out for the Read method, which is used to read the stream returned by the GetResponseStream method, and for the Write method, which is used to write to the stream returned by GetRequestStream method.

To specify the amount of time to wait for the request to complete, use the Timeout property.

Well, there’s the five minute timeout.  If you want to wait for a long request, which is greater than five minutes for the HttpWebRequest, you need to set both properties.  Using the information in the post from the summer, you can add code to your override of GetWebRequest() in your SOAP proxy to set the timeout to an hour, for example, using request.Timeout = 3600 * 1000 and request.ReadWriteTimeout = 3600 * 1000.

You may also notice that the Timeout property doesn’t apply to asynchronous calls that use the Begin/End call pattern.  For those, only the ReadWriteTimeout applies.

Topics
C#

Author

Buck Hodges
Director of Engineering

Director of Engineering, Azure DevOps

0 comments