Reputation: 21
Wondering if anyone could help me in understanding timeouts when HttpCompletionOption.ResponseHeadersRead is used. Given the code below:
using (HttpClient client = new HttpClient())
{
client.Timeout = TimeSpan.FromSeconds(5);
const string url = "https://url";
using (HttpResponseMessage response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead))
using (Stream stream = await response.Content.ReadAsStreamAsync())
{
... do some processing on stream ...
}
}
Is my understanding correct that:
client.Timeout = TimeSpan.FromSeconds(5)
)? and because HttpCompletionOption.ResponseHeadersRead
is used, client.GetAsync
is successfully completed once the headers are downloadedclient.Timeout = TimeSpan.FromSeconds(5)
configurationThanks.
Sorry, one other related question, say the stream processing consisted of a CopyTo call:
await stream.CopyToAsync(destStream);
Does the default CopyToAsync
call above (no cancellation token) have any sort of time restriction?
Upvotes: 2
Views: 366
Reputation: 5694
- the timeout of the client.GetAsync call is 5s (because of
client.Timeout = TimeSpan.FromSeconds(5)
)? and becauseHttpCompletionOption.ResponseHeadersRead
is used,client.GetAsync
is successfully completed once the headers are downloaded
Yes this is correct, the timeout will only affect how long it takes to get the status code and other headers back from the server. It doesn't matter if the server hasn't started writing any content to the response body.
- the stream processing is not affected by the
client.Timeout = TimeSpan.FromSeconds(5)
configuration
Yes. It won't matter how long it takes to read the response stream, your http request will never timeout unless you pass a CancellationToken to the await response.Content.ReadAsStreamAsync()
call.
- stream processing does not have a timeout / time limit (on the client side at least). so even for a very large file (say 1GB), it would theoretically be ok for the stream processing to take many minutes
Yes. This is true even if the server isn't writing anything to the response stream for some reason; the connection will stay alive until either the client or the server disconnects it. Or if the response is finished completely obviously.
Upvotes: 0