How exactly are timeouts performed by HttpClient accounted for?

So, there are two timeout properties that can be set on HttpClient : HttpClient.TimeOut and WebRequestHandler.ReadWriteTimeout .

The first one is just a timeout for the entire request / response, so if the download / download takes longer, Iโ€™m out of luck and is cut off in the middle of the transfer, and no questions were asked. Obviously, this can be overridden by setting the timeout to Infinite, but I'm not sure what the consequences are.

Now the last one (ReadWriteTimeOut) - at least to my understanding - should extend right up to NetworkStream , where it affects how long the request / response flow can block (idle) before the timeout expires.

HttpClient uses asynchronous versions of the HttpWebRequest methods, but as stated here :

In the case of asynchronous requests, the client application is responsible for implementing its timeout mechanism.

It doesnโ€™t explain at all what kind of timeout mechanism they have in mind (the idle timeout timeout of the network stream for the whole HTTP GET? Etc.), so it bothers me a lot.

So my question is: how exactly does HttpClient handle network / protocol timeouts? I want to use HttpClient to upload / download potentially large files, so for this I set HttpClient.TimeOut to infinity. But I am concerned that in doing so, the application runs the risk of endlessly waiting for any event that the server / network refuses to complete.

+7
c # networking
source share
2 answers

When downloading a file using HttpClient, there are 2 options

  • Use HttpCompletionOption.ResponseContentRead in the Get request (this is an implicit default option). Then HttpClient.Timeout effectively applied to the entire loading procedure. I believe the case when you thought of an infinite timeout

  • Explicitly using HttpCompletionOption.ResponseHeadersRead in your Get request. Then HttpClient.Timeout is only used to receive a head response without content. After that, you can download the content management timeout yourself, for example. on response.Content.ReadAsStreamAsync()

I believe option 2 is what you were looking for. Just set up HttpClient.Timeout to get a response from the server in a reasonable amount of time, and then download the content

0
source

Your link is for HttpWebRequest. However, HttpClient.Timeout (only works in .NET 4.5) for HttpClient. Thus, it handles the timeout when the HttpClient API is used. However, establishing infinity is bad practice. In this case, internal timeouts are used, which will lead to the exception of throwing after ~ 10 minutes (if the internal timeout values โ€‹โ€‹were not changed in .NET 4.5)

-one
source

All Articles