There is a range attribute in the HTTP request header information that can be used to request files in some HTTP requests. In. net, the range of data is defined using the httpwebrequest. addrange method.
After an HTTP request with the range attribute is sent, if the server supports this request, that is, partial data extraction is supported (this is also what we often say to support resumable download, for resumable download, a Range attribute is used to specify the range that is not downloaded.) The server returns the partial content status value. Otherwise, the OK status value (Code 200) is returned ). Note: If the server supports range but the range of the HTTP range request exceeds the file range, the server returns the requestedrangenotsatisfiable status value (Code 416 ).
Code:
// + Using system. net;
// + Using system. IO;
Static void main (string [] ARGs)
{
// Create an HTTP request
VaR request = (httpwebrequest) webrequest. Create ("http://files.cnblogs.com/mgen/mgen_amalon.zip ");
// From 3rd to 12th bytes, a total of 10 bytes. (0 is the first byte)
Request. addrange (2, 11 );
Try
{
// Obtain the HTTP response. Note that httpwebresponse inherits from idisposable.
Using (VAR response = (httpwebresponse) request. getresponse ())
{
If (response. statuscode = httpstatuscode. OK)
Throw new exception ("partial range download is not supported for files ");
// Set the buffer for receiving information
VaR bytes = new byte [5000];
Console. writeline ("Server Response: {0}", response. statuscode );
Console. writeline ("file size: {0}", humanreadablebytecount (response. contentlength, false ));
// Obtain the response stream (byte stream)
Using (VAR stream = response. getresponsestream ())
Using (VAR outstream = new memorystream ())
{
Long Recv = 0;
Int count;
// Pay attention to the actual size of received data during reading
While (COUNT = stream. Read (bytes, 0, 5000 ))! = 0)
{
Console. writeline ("downloaded: {0}", humanreadablebytecount (Recv + = count, false ));
// Write the received data
Outstream. Write (bytes, 0, count );
}
// Output the downloaded content
Console. writeline (bitconverter. tostring (outstream. toarray ()));
}
}
}
Catch (exception ex)
{
Console. writeline ("error message: {0}", Ex. Message );
}
}
// Normalize the output size
// The Code comes from/Original Source
// Http://stackoverflow.com/questions/3758606/how-to-convert-byte-size-into-human-readable-format-in-java
Public static string humanreadablebytecount (long bytes, bool Si)
{
Int unit = Si? 1000: 1024;
If (Bytes <unit) return bytes + "B ";
Int exp = (INT) (math. Log (bytes)/Math. Log (unit ));
String pre = (Si? "Kmgtpe": "kmgtpe") [exp-1] + (Si? "": "I ");
Return string. Format ("{0: F1} {1} B", Bytes/Math. Pow (unit, exp), pre );
}
Ten data in the middle will be downloaded:
If you try to download objects that do not support the range part, for example, the webpage of the URL specified by the common http get request. The program will output "the file does not support partial range download ".