C # programming Summary (12) resumable upload

Source: Internet
Author: User

We often use download tools, such as bit genie, thunder, and FlashGet, which support resumable data transfer. Resumable download: After the download task is paused, you can continue without re-Downloading. That is, you need to notify the server of the start position when downloading. If multiple threads are allowed for multipart download, the start-end position must be provided. In the end, you can choose to download a part. The entire object's byte stream can intercept the part of the stream, accumulate the stream, and finally download the object. I. principle added a header attribute in HTTP/1.1: Range, which is also the core of multi-thread download implemented by many so-called multi-thread download tools (such as FlashGet and thunder. Old versions of HTTP are not supported, so some old servers do not support resumable data transfer. Range (request parameter) is used in the request header to specify the location of the first byte and the location of the last byte. The general format is Range :( unit = first byte pos) -[last byte pos], for example, Range: 100-199, takes the bytes between 100 and 199 of the file stream. Range: 100. All bytes after the position is 100. If the range value is positive, the server should start sending data from the specified range parameter to the end of the data in the HTTP object. Range:-99, the first 100 bytes. If the range value is negative, the server should start sending data from the beginning of the data in the HTTP object to the specified range parameter. Content-Range (Response Parameter) is used to specify the insert position of a part of the entire object in the Response Header. It also indicates the length of the entire object. When the server returns a partial response to the customer, it must describe the response coverage and the length of the entire object. General Format: Content-Range: bytes (unit first byte pos)-[last byte pos]/[entity leader] example: Content-Range: bytes 1024000-1126399/7421120 HTTP protocol: http://www.w3.org/Protocols/rfc2616/rfc2616.html 2, C # implementation in C # using the AddRange method to add a specified range of bytes to the Request Header System. net. all HttpWebRequest Methods: common methods are used as an example: void AddRange (long from, long to); specify the start and end positions of the range to request the data of the segment. Copy the code // Summary: // Add the byte range header in the specified range to the request. //// Parameter: // from: // the location where data is sent. //// To: // the location where data sending is stopped. Public void AddRange (long from, long, displays the progress bar.: The Range parameter of the Request. We can clearly see the specific value, which is the Request segment. Here we can clearly see the HTTP protocol version, request method, request address and other information. The Response server returns only the data of this fragment according to the request Range parameter. We can clearly see the specific value of Content-Range. Note: The returned StatusCode is changed to PartialContent, which indicates part of the data. Code Description 200 the core code for downloading part of data in 206 Partial Content is returned for a successful OK request: copy the code /// <summary> /// Download /// </summary> public void Download () {// count from 0, you need to subtract a long from = this. currentSize; if (from <0) {from = 0;} long to = this. currentSize + this. step-1; if (to> = this. totalSize & this. totalSize> 0) {to = this. totalSize-1;} this. download (from, to);} // <summary> // Download /// </summary> // <param name = "url"> </para M> /// <param name = "range"> </param> public void Download (long from, long to) {if (this. totalSize = 0) {GetTotalSize ();} if (from> = this. totalSize | this. currentSize> = this. totalSize) {this. isFinished = true; return;} HttpWebRequest request = (HttpWebRequest) HttpWebRequest. create (url); // request. method = "GET"; request. addRange ("bytes", from, to); HttpWebResponse response = (HttpWebResponse) Request. GetResponse (); string result = string. Empty; if (response! = Null) {byte [] buffer = this. buffer; using (Stream stream = response. getResponseStream () {int readTotalSize = 0; int size = stream. read (buffer, 0, buffer. length); while (size> 0) {// write only the read bytes to the file fs. write (buffer, 0, size); readTotalSize + = size; size = stream. read (buffer, 0, buffer. length);} // update the current progress this. currentSize + = readTotalSize; // if the returned Content-Range value in the response header is null, the server does not support the Range attribute, does not support resumable data transfer, and returns all data if (response. headers ["Content-Range"] = null) {this. isFinished = true ;}}}}

Related Article

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.