Principle of. net resumable upload and. net resumable upload

Source: Internet
Author: User
Tags microsoft iis

Principle of. net resumable upload and. net resumable upload

Before learning about the principle of HTTP resumable upload, let's talk about the HTTP protocol. HTTP is a simple tcp-based protocol, divided into two types: request and reply. A request protocol is a protocol used by a client (browser) to send a message when a request is submitted to the SERVER (web server. The reply protocol is the protocol used by the server to reply packets to the client (browser. Both request and response protocols are composed of headers and bodies. The header and body are separated by a blank line.

 

The following is an example of a request message and the corresponding response message:

GET/image/index_r4_c1.jpg http/ 1.1

Accept :*/*

Referer: http: // 192.168.3.120: 8080

Accept-Language: zh-cn

Accept-Encoding: gzip, deflate

User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0;. net clr 1.0.3705)

Host: 192.168.3.120: 8080

Connection: Keep-Alive

 

 

HTTP/1.1 200 OK

Server: Microsoft-Microsoft IIS/5.0

Date: Tue, 24 Jun 2003 05:39:40 GMT

Content-Type: image/jpeg

Accept-Ranges: bytes

Last-Modified: Thu, 23 May 2002 03:05:40 GMT

ETag: "bec48eb862c21: 934"

Content-Length: 2827

 

Jfif h nbsp; C [1]

....

 

Next we will talk about "resumable upload ".

As the name suggests, resumable download starts when the previous download is disconnected. In the HTTP protocol, you can add a Range segment to the request header to indicate where the client wants to continue downloading.

For example, when downloading from 1,024th bytes, the request message is as follows:

 

GET/image/index_r4_c1.jpg http/ 1.1

Accept :*/*

Referer: http: // 192.168.3.120: 8080

Accept-Language: zh-cn

Accept-Encoding: gzip, deflate

User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0;. net clr 1.0.3705)

Host: 192.168.3.120: 8080

Value Range: bytes = 1024-

Connection: Keep-Alive

 

Related Classes in. NET

After understanding the above principles, let's take a look at the classes provided by. net framework for us to do these things.

Complete HTTP Request

System. Net. HttpWebRequest

HttpWebRequestProvides support for attributes and methods defined in WebRequest and additional attributes and methods that allow users to directly interact with servers using HTTP.

HttpWebRequestPublic HTTP header values sent to Internet resources are published as attributes and set by methods or systems. The following table contains the complete list. You can set other Headers in the Headers attribute to name/value pairs. However, note that some public headers are regarded as restricted. They can be disclosed directly by the API or protected by the system and cannot be changed. Range also belongs to the protected column, however ,. NET provides developers with more convenient operations, that is, the AddRange method, to add a byte range header from the beginning or end of the request data to the request

Complete File Access

System. IO. FileStream

FileStreamObjects support random access to files using the Seek method,SeekAllows you to move the read/write location to any location in the file. This is done through the parameter of the byte offset reference point. The Byte offset is relative to the search reference point. The reference point can be the start, current location, or end of the basic file, which are expressed by the three attributes of the SeekOrigin class.

 

Code Implementation

After learning about the classes provided by. NET, we can easily implement them. Http://hovertree.com/menu/dotnet/

The Code is as follows:

 

Static void Main (string [] args) {string StrFileName = "c: // aa.zip"; // set string StrUrl =" http://www.xxxx.cn/xxxxx.zip "; // Set according to the actual situation/* What is hovertree.com * // open the last downloaded file or create a new file long lStartPos = 0; System. IO. fileStream fs; if (System. IO. file. exists (StrFileName) {fs = System. IO. file. openWrite (StrFileName); lStartPos = fs. length; fs. seek (lStartPos, System. IO. seekOrigin. current); // Move the Current pointer in the file stream} else {fs = new System. IO. fileStream (StrFileName, System. IO. fileMode. create); lStartPos = 0;} // open the network connection try {System. net. httpWebRequest request = (System. net. httpWebRequest) System. net. httpWebRequest. create (StrUrl); if (lStartPos> 0) request. addRange (int) lStartPos); // sets the Range value // sends a request to the server to obtain the server response data stream System. IO. stream ns = request. getResponse (). getResponseStream (); byte [] nbytes = new byte [512]; int nReadSize = 0; nReadSize = ns. read (nbytes, 0,512); while (nReadSize> 0) {fs. write (nbytes, 0, nReadSize); nReadSize = ns. read (nbytes, 0,512);} fs. close (); ns. close (); Console. writeLine ("download completed");} catch (Exception ex) {fs. close (); Console. writeLine ("error occurred during download:" + ex. toString ());}}

Recommended: http://www.cnblogs.com/roucheng/p/netkuangjia.html

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.