This article is a mirror article of machine translation, please click here to jump to the original article.

View: 19401|Reply: 2

[ASP.NET] ASP.NET use HttpListener to resume file breakpoints

[Copy link]
Posted on 11/27/2019 10:42:41 AM | | |
The principle of breakpoint continuation is very simple, that is, the header of the HTTP request and reply message is different from the normal download.
When requesting a document on a server in the normal way, the request made and the server received are as follows:
request header:
Cache-Control: no-cache
Connection: close
Pragma: no-cache
Accept: */*
Host: localhost
response header:
200
Content-Type: application/octet-stream
Content-Disposition: attachment;FileName=time.pdf

When the server supports breakpoint resumption, the request and response are as follows:
request header:
Cache-Control: no-cache
Connection: close
Pragma: no-cache
Accept: */*
Host: localhost
Range: bytes=15360-
response header:
206
Content-Type: application/octet-stream
Content-Disposition: attachment;FileName=time.pdf

The different parts of the two messages are marked with red sections. It can be seen:
The Range header is used to identify the customer's desired download location.
When the server's answer number is 200, it means that the download starts from the file header, and 206 means that the transfer starts from a specific location of the file.
In other words, when supporting breakpoint resumption, you can start downloading from any part of the file, while the normal way can only download from the file header.
To make the server support breakpoint resumption, the following problems need to be solved:

1。 If it is a resumption request, you need to obtain the file range required by the client.
As you can see from the above analysis, when the client is transmitting a breakpoint, the Range field is added to the packet header, and you can determine whether it is a breakpoint transmission request as follows.
string range = request. Headers["Range"];
bool isResume = string. IsNullOrEmpty(range);

2。 Respond correctly to the client to notify the client server that the endpoint is supported for continuation
When sending a request for a breakpoint, the corresponding number for the client can be set as follows:
response. StatusCode = 206;

3。 Deliver the right content that the client needs
Delivering the correct content required by the client generally involves the following steps
Obtain the client's file request range by analyzing the range.
When a breakpoint is sent, the required length is shorter than the length of the file, so the correct response needs to be set. ContentLength64 property.
Transfer the required content correctly
Code example:






Previous:httplistener listens to get the POST request parameters
Next:18 philosophical comics, all of whom are masters
Posted on 11/27/2019 1:14:14 PM |
mark
Posted on 11/30/2019 12:04:39 PM |
marked      
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com