. Net (C #): use range in the httpwebrequest header to download file fragments.

Source: Internet
Author: User

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 ".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.