c implementation breakpoint continuation function sample sharing

  • 2020-06-15 10:05:39
  • OfStack

Before we understand the principle of HTTP breakpoint continuation, let's take a look at the HTTP protocol, which is a simple protocol based on tcp, divided into request and reply. Request protocol is a protocol that sends messages when a client (browser) submits a request to a server (WEBSERVER). The reply protocol is the protocol used by the server (webserver) to reply messages to the client (browser). Request and reply protocols are composed of heads and bodies. The head and body are separated by 1 line of empty behavior.

The following is an example of a request message and 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 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 implies, breakpoint continuation is the continuation of the download at the point where you were disconnected during the previous download.
In the HTTP protocol, the Range segment can be added to the request header to indicate where the client wants to continue downloading from.

For example, starting at byte 1024, the request message reads 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

With that in mind, let's take a look at the.NET FRAMEWORK classes that we can use to do this.

Complete the HTTP request

System.Net.HttpWebRequest

The HttpWebRequest class supports properties and methods defined in WebRequest, as well as additional properties and methods that enable users to interact directly with servers using HTTP.

HttpWebRequest exposes common HTTP header values sent to Internet resources as properties, set by methods or systems. The following table contains the complete list. You can set other headers in the Headers property to name/value pairs. Note, however, that certain public headers are considered restricted and are either directly exposed by API or protected by the system and cannot be changed. Range is also protected, however.NET provides a more convenient operation for developers, the AddRange method, which adds a byte range header to the request to complete file access from the beginning or end of the request data

System.IO.FileStream

The FileStream object supports random access to a file using the Seek method, which allows you to move the read/write location to any location in the file. This is done with byte offset reference point parameters. The byte offset is relative to the lookup reference point, which can be the beginning, current location, or end of the underlying file, represented by three attributes of the SeekOrigin class, respectively.


NET provides related classes, so, 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); // Moves 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(StrUrl);
if ( lStartPos>0)
request.AddRange((int)lStartPos); // Set up the Range value 
// Request to the server to get the server's 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(" The download is complete ");
}
catch(Exception ex)
{
fs.Close();
Console.WriteLine(" An error occurred during download :"+ex.ToString());
}
}


Related articles: