0

I am working on a multithread download manager. I want to divide the file into ranges to download with different threads. When a range download is completed I need to divide one of the remaining ranges into two ranges to keep the number of threads constant. So how to change a webrequests range after it started to download to make it stop on the end of the range? I have the code block below but it didn't work.

var webReq = WebRequest.Create(url) as HttpWebRequest;
webReq.AddRange(start, end);
var webResp = webReq.GetResponse() as HttpWebResponse;
webReq.AddRange(start, end2);
Ali Tor
  • 2,772
  • 2
  • 27
  • 58
  • Put the start and end into a POST. – jdweng Feb 02 '20 at 13:02
  • @jdweng do you mean webReq.Method = "POST" and webReq.AddRange? – Ali Tor Feb 02 '20 at 13:13
  • Yes. The start and end are parameter you need to put into a post. – jdweng Feb 02 '20 at 13:20
  • You mean `webReq.AddRange(start2, end2);`, where `start2 = end + 1`. This if a Server supports ranges, not to be taken for granted at all. You also need to know the content-length beforehand, no guarantees either. You could post a HEAD request, get the size and divide in chunks (corresponding to the number of Threadpool threads you want to spawn) if available, try a Rage request and see whether you get an `Accept-Ranges` header with the `byte` value set, proceed as usual if you don't. – Jimi Feb 02 '20 at 14:34
  • @Jimi, yes I know that Accept-Ranges must be equal to 'bytes' to set the ranges in the request. My main goal is to change the ranges after getting the response and I expect that the response content changes. – Ali Tor Feb 02 '20 at 17:39
  • As described: issue a HEAD request for a specific resource. If the Server answers setting the `Accept-Ranges: bytes` header, then ranges are operational. In this case, the Server (usually, but not granted) will also set the `Content-Length`. Now, you divide the Length by the number of Threadpool threads you mean to spawn, create a `List` (or whatever you have planned for) and pass to each Task (the method it runs) its range values. So you issue a new WebRequest for each chunk. You could use HttpClient instead WebRequest, but it'll work anyway. – Jimi Feb 02 '20 at 17:56
  • If the Server does not set the `Content-Length`, then you have a problem. You don't have much of a choice here. You probably need to download the whole thing in a single Task. – Jimi Feb 02 '20 at 17:59

0 Answers0