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.
thank
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
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 methodGetRequestStream
or read from a stream returned by a methodGetResponseStream
.In particular, the property
ReadWriteTimeout
controls the timeout for the methodRead
that is used to read the stream returnedGetResponseStream
, and for the methodWrite
that is used to write to the stream returned by the methodGetRequestStream
.Use the Timeout property to specify the time to wait for the request to complete .
source to share