Use .NET to achieve breakpoints
The principle of breakpoint resume
Before you understand the principles of HTTP breakpoint, the HTTP protocol is first said that the HTTP protocol is a simple protocol based on TCP and is divided into requests and reply. The request protocol is sent by the client (browser) to the server (Web Server). The reply protocol is the protocol when the server (web server) is replying to the client (browser). The request and reply protocol consists of the head and body. The head and body are separated by a row of empty behaviors.
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 200 ok
Server: 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 h C [1]
.
Let's talk about "breakpoint renewal".
As the name suggests, the breakpoint renewal is to continue to download when disconnected at the last download. In the HTTP protocol, you can join the RANGE segment in the request message, to indicate where the client wants to continue downloading.
For example, start downloading from the 1024th byte, request the packet 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
Understand the principles above, then let's see which classes provide us to do this in .NET Framework.
Complete HTTP request
System.Net.httpwebRequest
The HTTPWebRequest class provides support for the properties and methods defined in WebRequest, making it support for additional properties and methods that allow users to interact with server interactions with HTTP.
HTTPWebRequest will be disclosed by the public HTTP header value sent to the Internet resource, and is set by methods or system. The following table contains a complete list. You can set other headers in the Headers property to name / value pairs. Note that some public headers are considered to be restricted, or they are disclosed by the API, or are protected by system protection, cannot be changed. Range is also protected, but .NET provides developers with more convenient operation, is the AddRange method, add a specific range of byte range headers from the beginning of request data or end.
Complete file access
System.io.fileStreamFileStream object supports random access using the SEEK method, and Seek allows the read / write location to move to any location in the file. This is done by the byte offset reference point parameters. The byte offset is relative to the finding reference point, which can be the beginning, current location, or end of the base file, indicated by the three attributes of the SeekorigIn class.
Code
Understand the related classes provided by .NET, then we can be easily achieved.
code show as below:
Static void main (string [] args)
{
String strfilename = "c: //aa.zip"; // According to the actual situation
Strincel = "http://www.xxxx.cn/xxxxx.zip"; // 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); / / The current pointer in the 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 (Strull);
IF (LStartPOS> 0)
Request.addRange ((int) LStartPOS); // Setting the Range value
/ / Require the server to get 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 Done");
}
Catch (Exception EX)
{
fs.close ();
Console.writeline ("An error occurred during the download:" EX.TOSTRING ());
}
}
The above is a little experience in the development, I hope to share with you! :)
Note: This article must not be reproduced without permission