Search code examples
c#http-headershttpclient

HttpClient and HttpRequestHeaders.Range


I am trying to use HttpClient to download only part of a file (in this example, from the SEC Website).

If I set the RangeHeaderValue to something >= 4200, I get a part of the file (although response.Content.Headers.ContentLength says the size is 32628 bytes, which could be due to compression). If I watch the request go out in Fiddler, I see Range: bytes=0-4200 as a header under Miscellaneous. So I am fairly confident I am setting the headers correctly. What I cannot figure out is 2 fold, why does setting the max length on RangeHeaderValue to less than ~4200 result in a ContentLength of 0 (confirmed in Fiddler) and why does the ContentLength not match up to the requested range?

I have confirmed (by looking at the headers) that the SEC server supports ranges (Accept-Ranges: bytes). Sample code is below.

var client = new HttpClient(new HttpClientHandler { AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip });
var request = new HttpRequestMessage { RequestUri = new Uri("http://www.sec.gov/Archives/edgar/full-index/2013/QTR1/company.idx") };
request.Headers.Range = new RangeHeaderValue(0, 1000);

Console.WriteLine(request.Headers.Range.Ranges);
var response = await client.SendAsync(request);
Console.WriteLine(response.Headers);
Console.WriteLine(response.Content.Headers.ContentLength);
Console.WriteLine(response.RequestMessage);

OutputFromProgram


Solution

  • There is no question here, the server claims it obeys the range standard but appears to ignore it. Further research using several CDNs, the code works properly.