0

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

8
  • Does req.Headers.Add("Range", "bytes"); work better? Commented Dec 12, 2021 at 18:10
  • @LajosArpad How will I specify the range numbers then? Commented Dec 12, 2021 at 18:16
  • Your code doesn't compile and once fixed, doesn't produce the response you posted. It actually returns 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 code Commented Dec 12, 2021 at 18:38
  • PS: Don't use GetAwaiter().GetResult(). Use async/await instead. You can use async Task Main(string[] args) in your Main method, so there's seldom any reason to block Commented Dec 12, 2021 at 18:41
  • @PanagiotisKanavos When a console app's Main becomes async, it just return to console prompt when the line of await reached. I just don't like that behaviour because it seems out of my control, so here comes the good old GetAwaiter. Commented Dec 13, 2021 at 16:50

1 Answer 1

3

RangeHeaderValue appears to be working as expected under net5.0:

var url = "https://us.download.nvidia.com/Windows/497.09/497.09-desktop-win10-win11-64bit-international-dch-whql.exe";
var client = new HttpClient();
var req = new HttpRequestMessage { RequestUri = new Uri( url ) };
req.Headers.Range = new RangeHeaderValue( 0, 999 );

var resp = await client.SendAsync( req );

Console.WriteLine( $"\r\nResponse:\r\n{resp}" );

With the Response Output:

StatusCode: 206, ReasonPhrase: 'Partial Content', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
{
  Accept-Ranges: bytes
  Age: 18147
  Cache-Control: max-age=86400
  Date: Sun, 12 Dec 2021 18:25:42 GMT
  ETag: "1774295931"
  Server: ECAcc
  Server: (daa/7CFD)
  X-Cache: HIT
  x-vdms-version: 117
  Content-Range: bytes 0-999/870849168
  Content-Type: application/octet-stream
  Expires: Mon, 13 Dec 2021 18:25:42 GMT
  Last-Modified: Wed, 01 Dec 2021 12:42:07 GMT
  Content-Length: 1000
}

Upon further inspection of your posted code:

var resp2 = respTask2.GetAwaiter().GetResult();
WriteLine(resp.ToString() );

Notice the WriteLine is for resp.ToString(), but should be using resp2.ToString(). So, it appears you're actually printing out some other response.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.