Riyafa Abdul Hameed
Riyafa Abdul Hameed

Reputation: 7983

Why does ReadAsStreamAsync continue download after connection drops and reconnects?

I'm downloading a large file using the following code:

using var response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
    await using var fileStream = new FileStream(localFilePath, FileMode.Append,FileAccess.Write, FileShare.None);
await using var contentStream = await response.Content.ReadAsStreamAsync(cancellationToken);
await contentStream.CopyToAsync(fileStream, 81920, cancellationToken);

I fail to understand why this continues to download the file even if the connection drops and reconnects. I tried to set the following to stop it retrying

httpClient.DefaultRequestHeaders.ConnectionClose = true;

but it still continues to download after connection drops and reconnects.

For one file I disconnected for more than 5 minutes before reconnecting and it still continued to download.

What's it in the HttpClient that makes this possible?

Upvotes: 0

Views: 21

Answers (0)

Related Questions