With this curl command:
> curl --range 0-999 -I https://us.download.nvidia.com/Windows/497.09/497.09-desktop-win10-win11-64bit-international-dch-whql.exe
The response correctly returns as:
HTTP/1.1 206 Partial Content
Accept-Ranges: bytes
Cache-Control: max-age=86400
Content-Range: bytes 0-999/870849168
Content-Type: application/octet-stream
Date: Sun, 12 Dec 2021 17:38:31 GMT
Etag: "1774295931"
Last-Modified: Wed, 01 Dec 2021 12:42:07 GMT
Content-Length: 1000
When using the .NET HttpClient, it appears to fail with a range request header:
var url = "https://us.download.nvidia.com/Windows/497.09/497.09-desktop-win10-win11-64bit-international-dch-whql.exe";
var req = new HttpRequestMessage();
req.Method = HttpMethod.Get;
req.RequestUri = new Uri(url);
// req.Headers.Range = new RangeHeaderValue(0, 999); // not working
req.Headers.Add("Range", "bytes=0-999"); // not working
WriteLine(req.ToString() );
var client2 = new HttpClient();
var respTask2 = client2.SendAsync(req, HttpCompletionOption.ResponseHeadersRead);
var resp2 = respTask2.GetAwaiter().GetResult();
WriteLine(resp.ToString() );
The HttpClient response returns as:
Method: GET, RequestUri: 'https://us.download.nvidia.com/Windows/497.09/497.09-desktop-win10-win11-64bit-international-dch-whql.exe', Version: 1.1, Content: <null>, Headers:
{
Range: bytes=0-999
}
StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
{
Accept-Ranges: bytes
Date: Sun, 12 Dec 2021 17:44:07 GMT
ETag: "1774295931"
Content-Type: application/octet-stream
Last-Modified: Wed, 01 Dec 2021 12:42:07 GMT
Content-Length: 870849168
}
How do you use the Range header with HttpClient request? I wonder if this is a bug or what?
Reference: HttpRequestHeaders.Range Property
req.Headers.Add("Range", "bytes");work better?Content-Length: 1000. It look like you've mixed up multiple requests and responses and print the wrong response. Don't rush to assume something used by every .NET Core developer is broken. Create a new empty project and add the minimal code necessary to reproduce the bug. If you can't, the problem is in the rest of your codeGetAwaiter().GetResult(). Useasync/awaitinstead. You can useasync Task Main(string[] args)in your Main method, so there's seldom any reason to blockMainbecomesasync, it just return to console prompt when the line ofawaitreached. I just don't like that behaviour because it seems out of my control, so here comes the good oldGetAwaiter.