Use. NET implementation of breakpoint continuation

Source: Internet
Author: User

Http://www.cnblogs.com/goody9807/archive/2007/06/05/772501.html

How the breakpoint continues to pass
Before understanding the principle of HTTP breakpoint continuation, first of all, the HTTP protocol, the HTTP protocol is a simple TCP-based protocol, divided into two types of requests and replies. A request protocol is a protocol that sends a message when a client (browser) submits a request to the server (WEB server). The reply protocol is the Protocol that is provided by the server (Web server) when replying to a message to the client (browser). Both the request and the reply agreement are made up of the headers and bodies. The head and body are separated by a line of empty behavior.

Here is an example of a request message with the corresponding reply 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 OK

server:microsoft-iis/5.0

Date:tue, June 2003 05:39:40 GMT

Content-type:image/jpeg

Accept-ranges:bytes

Last-modified:thu, 2002 03:05:40 GMT

ETag: "bec48eb862c21:934"

content-length:2827

? JFIF h H C [1]

....

Let's say "breakpoint continuation".

As the name implies, the continuation of a breakpoint is the location where the last download was broken. In the HTTP protocol, a range segment can be added to the request header to indicate where the client wants to proceed with the download.

For example, starting with the 1024th byte download, 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

range:bytes=1024-

Connection:keep-alive

. The related classes in net
Understanding the above principles, let's look at what classes are available in the. NET framework to do these things.

Completing the HTTP request
System.Net.HttpWebRequest

The HttpWebRequest class provides support for properties and methods defined in WebRequest, as well as additional properties and methods that enable users to interact directly with servers that use HTTP.

HttpWebRequest public HTTP header values that are sent to an Internet resource are exposed as properties, set by method or system. The following table contains the full list. You can set other headers in the Headers property to name/value pairs. Note, however, that some public headers are considered restricted and are either exposed 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 a more convenient way to do this is to add a AddRange method to the request, adding a specific range of byte-range headers from the beginning or end of the request data

Complete file access
System.IO.FileStream

The FileStream object supports random access to a file using the Seek method, and seek allows the read/write location to be moved to any location in the file. This is done through the byte offset reference point parameter. The byte offset is relative to the lookup reference point, which can be the beginning, current position, or end of the underlying file, represented by three properties of the SeekOrigin class, respectively.

Code implementation
Understand. NET provides the related classes, then, we can easily implement.

The code is as follows:

static void Main (string[] args)

{

String Strfilename= "C:\\aa.zip"; Set according to the actual situation

String strurl= "Http://www.xxxx.cn/xxxxx.zip"; Set according to the actual situation

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 a file stream

}

Else

{

FS = new System.IO.FileStream (strfilename,system.io.filemode.create);

Lstartpos = 0;

}

Open Network Connection

Try

{

System.Net.HttpWebRequest request = (System.Net.HttpWebRequest) System.Net.HttpWebRequest.Create (strURL);


if (lstartpos>0)

Request.    AddRange ((int) lstartpos); Set Range value

Request to server for server response data flow

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 Complete");

}

catch (Exception ex)

{

Fs. Close ();

Console.WriteLine ("Error occurred during download:" +ex. ToString ());

}

}

The above is my development in a little bit of experience, I hope to share with you! :)

Use. NET implementation of breakpoint continuation

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.