Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 422
Pete Garafano Avatar asked Sep 10 '13 21:09

Pete Garafano


2 Answers

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.

like image 164
Pete Garafano Avatar answered Oct 19 '22 22:10

Pete Garafano


Confirmed the server still hasn't implemented partial downloads (byte ranges). I've set Range: bytes=0-4201 & Range: bytes=0-1000 and all the time I get the full file: 45,846,783 bytes.

One way to get partial file is to pause/stop download when you've got enough bytes. This would however, work for byte range beginning with zero (first few bytes of file), not for middle portion of file.

like image 27
Zimba Avatar answered Oct 20 '22 00:10

Zimba