My program uses HttpClient to send a GET request to a Web API, and this returns a file.
I now use this code (simplified) to store the file to disc:
p
Instead of GetAsync(Uri)
use the the GetAsync(Uri, HttpCompletionOption)
overload with the HttpCompletionOption.ResponseHeadersRead
value.
The same applies to SendAsync
and other methods of HttpClient
Sources:
The returned Task object will complete based on the completionOption parameter after the part or all of the response (including content) is read.
.NET Core implementation of GetStreamAsync
that uses HttpCompletionOption.ResponseHeadersRead
https://github.com/dotnet/corefx/blob/release/1.1.0/src/System.Net.Http/src/System/Net/Http/HttpClient.cs#L163-L168
HttpClient spike in memory usage with large response
ResponseHeadersRead
is what does the trick)