1

I'm writing some code in C# to download files from the web servers.

I'm testing with some links which support a 'byte-range' request, so not sure what would happen with unsupported servers.

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("someUrl");
request.AddRange(someValue);

As you can see, nothing special. After getting the response from the server, it starts to download a file. I know servers that don't support byte ranges will return the status code 200 instead of 206. So, in that case, I'm curious if I have to retry without a byte range request again.

I hope I don't have to, because in some cases, such as google drive links, it takes too long for closing the response and getting another response. If a server doesn't support byte ranges, then I don't really mind downloading from the beginning. Does this happen automatically without coding?

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
Jenix
  • 2,996
  • 2
  • 29
  • 58

1 Answers1

1

So, in that case, I'm curious if I have to retry without a byte range request again

No, if the server doesn't support byte ranges, it will simply send the entire stream back to the client. It's as if the Range HTTP request header wasn't present. So basically when you are reading the response stream on the client you will get the entire file.

So if you detect a 200 status code (in contrast to 206 Partial Content) to a request you have made which includes byte ranges, you should not bother sending additional ranged requests because the server will already send you the entire file.

By the way in order to detect if a server supports ranged downloads you might consider sending an initial HEAD request to determine capabilities.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I thought so but couldn't be sure, thanks!! and appreciate the tip! – Jenix Jan 05 '16 at 14:26
  • Could I ask one more question? Some server returns the 206 status code and the value of "Content-Range" in the response header. But I can't find "Accept-Ranges: bytes" from the header. Then this means the server supports ranged download? – Jenix Jan 05 '16 at 14:44
  • 1
    Yes and no. You can't be sure. It means that the server committed a protocol violation. Normally the `Accept-Ranges: bytes` header should be present in the response. But the fact that it isn't present and the `Content-Range` header is present probably means that the server supports ranged downloads. – Darin Dimitrov Jan 05 '16 at 15:14