I am using HttpWebRequest with the AddRange function like:
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(URL);
myHttpWebRequest.AddRange(20, 30);
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
Stream streamResponse = myHttpWebResponse.GetResponseStream();
SaveFileStream(name, streamResponse); //save file function
...but the entire file downloaded.
AddRange() in the above code expects the bytes between 20 and 30 to be downloaded (in other words, to download those 10 bytes from the file).
But my code is not working, since the download is not segmented. This link provides an example: http://stackoverflow.com/robots.txt That file was downloaded in its entirety. Why?
HTTP server are not required to support Range
header requests. You can verify server's range support by issuing HEAD request and checking value of Accept-Ranges
header in response (see HTTP range requests). But this still doesn't guarantee that server will not ignore Range
header, in particular for very small ranges (it would be very inefficient for HTTP server to serve content in such small segments).
From RFC7233:
Because servers are free to ignore Range, many implementations will simply respond with the entire selected representation in a 200 (OK) response. That is partly because most clients are prepared to receive a 200 (OK) to complete the task (albeit less efficiently) and partly because clients might not stop making an invalid partial request until they have received a complete representation. Thus, clients cannot depend on receiving a 416 (Range Not Satisfiable) response even when it is most appropriate.
To determine if server accepted or ignored Range
header, you must check response status code. 200 (OK)
indicates that server ignored Range
header and returned whole response body, 206 (Partial Content)
indicates that range specified in header was returned by server and 416 (Range Not Satisfiable)
indicates that the set of ranges requested has been rejected due to invalid ranges or an excessive request of small or overlapping ranges.
In case of http://stackoverflow.com/robots.txt, server indicates support of Range
header by returning Accept-Ranges: bytes
header in response on HEAD
request, but on GET
request with AddRange(20, 30) specified, response is 200 (OK)
, so server just ignored such small range a returned whole reponse body. You have to cut requested range from response yourself, if you need to.