How can I use C# HttpClient to download a part of large file, like HttpWebRequest.AddRange(123)?
public async void StartDownload(CancellationToken cancellationToken)
{
try
{
if (_isWork)
return;
_isWork = true;
using (var response = await GetAsync(_doenloadUrl, HttpCompletionOption.ResponseHeadersRead, cancellationToken))
await DownloadFileFromHttpResponseMessage(response);
}
catch (Exception e)
{
downloadExceptiondHandler?.Invoke(_doenloadUrl, e);
}
}
There is a
Rangeheader specified in the HTTP protocol for that very purpose:Therefor you cannot rely on the server returning only that range of the file.
However, in a
HttpRequestthere is a propertyRangeinHttpRequestHeadersto set theRangeof a request