HttpWebResponse.ReadTimeout - Timeouts not supported?

We have an issue where, in one instance of our product, we get an InvalidOperationException when we try to set the ReadTimeout property of the System.Net.HttpWebResponse object.

This problem only occurs in one instance where we have many different sites without this problem. We tried to recreate the problem locally, to no avail.

The following code illustrates the problem.

Any ideas are appreciated.


    private static XmlReader GenerateReaderFromResponse(HttpWebResponse response, HttpWebRequest request)
        Stream responseStream = response.GetResponseStream();
        responseStream.ReadTimeout = request.Timeout; //This is where the exception is generated - System.InvalidOperationException: Timeouts are not supported on this stream.

        using (StreamReader responseReader = new StreamReader(responseStream, System.Text.Encoding.UTF8))
            XmlReaderSettings readerSettings = new XmlReaderSettings();
            readerSettings.ProhibitDtd = false;
            string responseContent = responseReader.ReadToEnd();
            return XmlReader.Create(new StringReader(responseContent), readerSettings);



source to share

2 answers

What you need is the HttpWebRequest.ReadWriteTimeout property .

Specifies the number of milliseconds before a read (or write) operation in the response response Stream

, throwing WebException

with Status

set to WebExceptionStatus . RequestCanceled


From the msdn documentation :

The property is ReadWriteTimeout

used when writing to a stream returned by a method GetRequestStream

or read from a stream returned by a method GetResponseStream


In particular, the property ReadWriteTimeout

controls the timeout for the method Read

that is used to read the stream returned GetResponseStream

, and for the method Write

that is used to write to the stream returned by the method GetRequestStream


Use the Timeout property to specify the time to wait for the request to complete .



First, make sure that responseStream

and request

are not null.

To them, you need to ensure that the server responds to your request after trying to read the response from it.

If you can, also include the code that makes the request.



All Articles