Use. NET to implement breakpoint continuation __.net

Source: Internet
Author: User
Use. NET to implement breakpoint continuation
The principle of continuous transmission of breakpoints
Before understanding the principle of HTTP breakpoint continuation, the HTTP protocol is a simple protocol based on TCP, which is divided into two kinds: request and reply. A request protocol is a protocol that sends a message when a client (browser) submits a request to the server (WEB server). A reply protocol is a protocol that the server (Web server) responds to a message to a client (browser). Both the request and reply protocols are composed of a header and a body. The head and body are separated by a line of empty behavior.
The following is an example of a request message and a 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, May 2002 03:05:40 GMT
ETag: "bec48eb862c21:934"
content-length:2827
? JFIF H hc [1]
....

Now let's say "breakpoint continuation".
As the name suggests, the continuation of a breakpoint is the last time the download was disconnected at the beginning of the download. In the HTTP protocol, you can include a range segment in the request message header to indicate where the client wants to continue downloading.
For example, starting from 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
. Related classes in net
Understanding the principle above, let's look at what classes in the. NET framework are available for us to do.
Complete HTTP request
System.Net.HttpWebRequest
The HttpWebRequest class provides support for the 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 exposes public HTTP header values sent to Internet resources as properties, by method or system settings. The following table contains the complete list. You can set other headers in the Headers property to name/value pairs. Note, however, that some public headers are considered restricted and either are exposed directly by the API or are protected by the system and cannot be changed. Range also belongs to the protected list, however,. NET provides a more convenient operation for developers, the AddRange method, which adds a range of bytes from the beginning or end of the request data to the request header
Complete file access
System.IO.FileStream
The FileStream object supports random access to a file using the Seek method, which 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 start, current position, or end of the underlying file, represented by the three properties of the SeekOrigin class, respectively.
Code implementation
Get it. NET provides the related class, then we can easily implement.
The code is as follows:

static void Main (string[] args)
{

String Strfilename= "C://aa.zip"; According to the actual situation set
String strurl= "Http://www.xxxx.cn/xxxxx.zip"; According to the actual situation set

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); Moving 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 to get 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 Complete");
}
catch (Exception ex)
{
Fs. Close ();
Console.WriteLine ("Error occurred during download:" +ex.) ToString ());
}
}

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.