The principle of Asp.net breakpoint continuation is Shared with the implementation method

  • 2020-05-17 05:07:55
  • OfStack

Request protocol is a protocol for sending messages when a client (browser) submits a request to a server (WEB SERVER). The reply protocol is the protocol used by the server (web server) to reply messages to the client (browser). Request and reply protocols are composed of headers and bodies. The head and body are separated by 1 line of empty action.
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 
 ... . 

As the name implies, a breakpoint continuation is the continuation of the download at the same location as the last download. In the HTTP protocol, an Range segment can be added to the request header to indicate where the client wants to continue the download.
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 

The corresponding response message is
 
HTTP/1.1 206 Partial Content 
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: 1803 
Content-Range: bytes 1024-1803/2827 

It can be seen from the two different packets that when a breakpoint is resumed, as long as we can give the client the corresponding message, so that the client can respond correctly, and send part of the files after the continuation point, the breakpoint can be resumed.
1. Distinguish between interrupted and continued messages.
Since the Range field is included in the breakpoint continuation message, it is only necessary to pass Request.Headers ["Range"] as null.
2. Send the correct relay response message
The different parts of the two response messages have been identified in the red part of the message. The correct message continuation can be sent by modifying the red part of the message header.
3. Send the correct part of the document
When the file is transferred, only the file after the transfer point is needed. First, the starting position of the file is obtained through the Range field in the request message. When the file is transferred, only the part after the location is needed.
The following code example shows an ASP.NET page that supports breakpoint continuation
 
private void Page_Load(object sender, System.EventArgs e) 
{ 
string file = MapPath("ff.zip"); 
FileInfo fi=new FileInfo (file); 

long startPos = 0; 

// The length of the file transferred  
long fileTranLen = fi.Length; 

// A breakpoint continues the request  
if (Request.Headers["Range"] != null) 
{ 
Response.StatusCode = 206; 
startPos = long.Parse(Request.Headers["Range"].Replace("bytes=", "").Split('-')[0]); 
fileTranLen -= startPos; 

//Response.AddHeader("Accept-Ranges", "bytes"); 
//Content-Range: bytes [ The first byte of a file block ]-[ The total size of the transfer file ]/[ The total size of the file ] 
Response.AddHeader("Content-Range", string.Format("bytes {0}-{1}/{2}",startPos,fileTranLen,fi.Length)); 
} 

Response.AddHeader("Content-Length", fileTranLen.ToString()); 

// Basic file download header  
Response.ContentType = "application/octet-stream"; 
Response.AddHeader("Content-Disposition", "attachment; filename=" + fi.Name); 

// Simple stream copy  
System.IO.Stream fileStream = System.IO.File.OpenRead(file); 
fileStream.Position = startPos; 

byte[] buffer = new Byte[1024]; 
int count; 
while ((count = fileStream.Read(buffer, 0, buffer.Length)) > 0) 
{ 
Response.OutputStream.Write(buffer, 0, count); 
} 
fileStream.Close(); 

Response.End(); 
} 

Related articles: