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);

source share