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 RangeHeaderValue to something> = 4200, I will get part of the file (although response.Content.Headers.ContentLength says the size is 32628 bytes, which may be due to compression). If I look at the request to Fiddler, I see Range: bytes=0-4200 as the header under Miscellaneous . So I'm sure I'm setting the headers correctly. What I can’t understand is 2 times, why setting the maximum length on RangeHeaderValue to less than 4200 results in ContentLength 0 (confirmed in Fiddler) and why ContentLength does not match the range request?

I confirmed (by looking at the headers) that the SEC server supports ranges ( Accept-Ranges: bytes ). Sample code 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

+6
source share
2 answers

There are no questions here, the server claims to comply with the range standard, but seems to ignore it. Further research using multiple CDNs, the code works correctly.

+3
source

It is confirmed that partial downloads (byte ranges) are still not implemented on the server. I 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 a partial file is to pause / stop the download when you have enough bytes. This, however, will work for a range of bytes starting from zero (the first few bytes of the file), and not for the middle part of the file.

0
source

Source: https://habr.com/ru/post/1501559/


All Articles