Before understanding the principle of HTTP breakpoint continuation, let us first understand the HTTP protocol. HTTP protocol is a simple protocol based on tcp, divided into two types: request and reply. The request protocol is a protocol that sends messages when a client (browser) submits a request to the server (WEBSERVER). The reply protocol is the protocol when the server (webserver) replys to the client (browser). The request and reply protocols are composed of heads and bodies. 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 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
Let’s talk about “breakpoint continuation”. As the name suggests, breakpoint continuation means that the download starts to continue downloading at the disconnected position during the last download.
In the HTTP protocol, a Range segment can be added to the request message header to indicate where the client wants to continue downloading.
For example, starting from byte 1024, 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
After understanding the above principle, let's take a look at what classes are provided in .NET FRAMEWORK to do these things.
Complete HTTP request
The HttpWebRequest class provides support for properties and methods defined in WebRequest, and also provides support for additional properties and methods that enable users to interact directly with servers using HTTP.
HttpWebRequest The public HTTP header value sent to an Internet resource is exposed as a property, set by a method or system. The following table contains the complete list. Other headers in the Headers property can be set to name/value pairs. However, note that certain public headers are considered restricted, they are either directly exposed by the API or protected by the system and cannot be changed. Range is also among the protected ones, but .NET provides developers with a more convenient operation, which is to add a specific range of byte range headers to the request from the beginning or end of the request data to complete file access.
The FileStream object supports random access to files using the Seek method, which allows the read/write location to be moved to any location in the file. This is done by the byte offset reference point parameter. The byte offset is relative to the search reference point, which can be the beginning, current position or end of the base file, respectively represented by three properties of the SeekOrigin class.
After understanding the relevant classes provided by .NET, we can easily implement them.
The code is as follows:
static void Main(string[] args)
{
string StrFileName="c:\\"; //Set according to actual situation
string StrUrl="/"; //Set according to actual situation
//Open the last downloaded file or create a new file
long lStartPos =0;
fs;
if ((StrFileName))
{
fs= (StrFileName);
lStartPos=;
(lStartPos,); //Move the current pointer in the file stream
}
else
{
fs = new (StrFileName,);
lStartPos =0;
}
//Open the network connection
try
{
request =()(StrUrl);
if ( lStartPos>0)
((int)lStartPos); //Set Range value
//Request from the server to get the server response data flow
ns= ().GetResponseStream();
byte[] nbytes = new byte[512];
int nReadSize=0;
nReadSize=(nbytes,0,512);
while( nReadSize >0)
{
(nbytes,0,nReadSize);
nReadSize=(nbytes,0,512);
}
();
();
("Download Complete");
}
catch(Exception ex)
{
();
("Error occurred during downloading:"+());
}
}