[Add to favorites] use. Net for resumable upload

Source: Internet
Author: User
Tags microsoft iis

Principle of resumable Data Transfer
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 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

The httpwebrequest class supports the attributes and methods defined in webrequest and the additional attributes and methods that allow users to directly interact with servers using HTTP.

Httpwebrequest exposes the public HTTP header value sent to Internet resources as an attribute and is set by the method or system. 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

The filestream object supports random access to the file using the seek method. The seek allows 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.

 

CodeImplementation
After learning about the classes provided by. net, we can easily implement them.

The Code is as follows:

 

Static void main (string [] ARGs)

{

String strfilename = "C: \ aa.zip"; // set it based on the actual situation

String strurl = "http://www.xxxx.cn/xxxxx.zip"; // set as needed

 

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

// Request from 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 ());

}

}

 

The above is my little experience in development and I hope to share it with you! :)

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.