Whats the difference between HttpClient.Timeout and using the WebRequestHandler timeout properties?
Solution 1
When you perform a SendAsync
the HttpClient.Timeout
is placed on the CancellationTokenSource
. This means this timeout is for the entire async operation.
On the other hand, WebRequestHandler.ReadWriteTimeout
is copied to the HttpWebRequest
where it is set on the request stream both ReadTimeout
and WriteTimeout
. So this is more a timeout at the stream level, which is ultimately a socket level timeout.
If you set both, then if the operation takes more than HttpClient.Timeout
in total it will timeout, and if a read or write from the stream takes longer than WebRequestHandler.ReadWriteTimeout
it will also timeout. Though I am not sure if there is a difference in the timeout exceptions raised.
Solution 2
WebRequestHandler.ReadWriteTimeout
- Gets or sets a time-out in milliseconds when writing a request to or reading a response from a server.
HttpClient.Timeout
- Gets or sets the TimeSpan
to wait before the request times out.
Here, WebRequestHandler
is a wrapper over HTTPClient
WebRequestHandler
derives from HttpClientHandler
but adds properties that generally only are available on full .NET.
To conclude, it is more on less same thing.
For more info refer this link - http://blogs.msdn.com/b/henrikn/archive/2012/08/07/httpclient-httpclienthandler-and-httpwebrequesthandler.aspx
iguanaman
Updated on September 13, 2020Comments
-
iguanaman over 3 years
I can set the timeout of my
HttpClient
object directly withHttpClient.Timeout
but I've recently read about theWebRequestHandler
class which is a derivative ofHttpClientHandler
.WebRequestHandler
has aReadWriteTimeout
property. How will this affect the operation of the request when used alongsideHttpClient.Timeout
? -
iguanaman over 9 yearsSince it's related to streams, would
ReadWriteTimeout
affectResponse.Content.ReadAsStringAsync
then? At the moment I useHttpCompletionOption.ResponseContentRead
when callingSendAsync
to make sure the content is read before the timeout, becauseReadAsStringAsync
doesn't allow an explicit timeout or cancellation. I'd like to useHttpCompletionOption.ResponseHeadersRead
instead, ifWebRequestHandler
allows me to set a timeout on reading the content stream. -
weston over 9 yearsIt's a bit hard to picture your use case from the description, can you post the relevant code by editing the question?