Use Visual C # to achieve breakpoints 2004-03-26 ■ Gu Junyan ■ Yesky Before understanding the principle of HTTP breakpoint, let us first understand the HTTP protocol, HTTP protocol is a simple protocol based on TCP, division Two kinds for 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.1Accept: * / * Referer: http://192.168.3.120:8080Accept-Language: zh-cnAccept-Encoding: gzip, deflateUser-Agent: Mozilla / 4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.0.3705) Host: 192.168.3.120:8080connection: Keep-alive
HTTP / 1.1 200 OKServer: Microsoft-IIS / 5.0Date: Tue, 24 Jun 2003 05:39:40 gmtcontent-type: image / jpegaccept-ranges: Byteslast-Modified: Thu, 23 May 2002 03:05:40 gmtetag: " BEC48EB862C21: 934 "Content-Length: 2827
.
Let's talk about "breakpoint renewal", as the name suggestion, the breakpoint renewal is the start of the disconnected position 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.1Accept: * / * Referer: http://192.168.3.120:8080Accept-Language: zh-cnAccept-Encoding: gzip, deflateUser-Agent: Mozilla / 4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.0.3705) Host: 192.168.3.120:8080Range: objects=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, and 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.FileStream
The FileStream 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"; // Set string strurl = "http://www.xxxx.cn/xxxxx.zip" according to the actual situation; // 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 (lstractpos, system.io.seekorigin.current); // The current pointer in // mobile 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 (Strull); if (LStartPos> 0) Request.addrange (int) LStartPOS); // Set the Range value
/ / Requirever to the server, 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 completion");} catch (exception ex) {fs.close (); console.writeLine ("Download Error: " ex. TString ());}}
The above is a little experience in the development, I hope to share with you!