How to correctly configure HttpClient's streaming for large files?

Viewed 1069

Microsoft's official documentation for HttpClient states that if we'd like to use HttpClient to download large files, we should

stream those downloads and not use the default buffering. If the default buffering is used the client memory usage will get very large, potentially resulting in substantially reduced performance.

What's the default buffering, and how do we change it, so that we won't have problems hitting the outlined problem above, regardless of the file size we end up downloading?

A dummy code snippet would be highly appreciated!

1 Answers

You have to use the GetStreamAsync method. As stated in the documentation:

This operation will not block. The returned Task object will complete after the response headers are read. This method does not read nor buffer the response body.

example:

HttpClient httpClient = new HttpClient();

var requestUri = "http://url-to-resource.com";
var stream = await httpClient.GetStreamAsync(requestUri);

using (var fileStream = File.Create("outputFile.ext"))
{
    await stream.CopyToAsync(fileStream);
}

All the other methods like GetByteArrayAsync or GetStringAsync will buffer the response and will complete after the whole response body is read.

The default buffer size used by CopyToAsync is 81920 bytes as declared by _DefaultCopyBufferSize. You can change it by using the overload CopyToAsync(Stream, Int32).

Related