In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
Note that the breakpoint continuation mentioned in this article refers specifically to the breakpoint continuation in the HTTP protocol. This article focuses on ideas and key code. For more details, please refer to the demo attached to this article.
working principle
Some request / response headers are defined in the HTTP protocol, and these headers are used by combining them. We can request only a portion of the data in a file in a HTTP request. In this way, we can save the downloaded data, just request the remaining data next time, and complete the merge when all the data is downloaded locally.
The HTTP protocol states that the scope of the request data can be specified through the Range header in the HTTP request, and the use of the Range header is also very simple, as long as you specify the following format:
Range: bytes=500-999
It means that only 500 to 999 bytes of the target file are requested.
For example, I have a file with a size of 1000 bytes to download. The first request does not have to specify a Range header, which means to download the whole file. However, after the 499th byte of the download, the download was cancelled. Then the next time you request to download the same file, you only need to download the 500th to 999th bytes of data. The principle looks simple, but we need to consider the following questions:
1. Do all web servers support Range headers?
2. There may be a long interval between multiple requests. What if the files on the server have changed?
3. How to save some of the downloaded data and related information?
4. When we spell a file into the original size through byte operation, how do we verify that it is exactly the same as the source file?
Let's take these questions to explore some of the details of the breakpoint continuation.
Check the server-side support for breakpoint continuation
When the server responds to our request, it uses Accept-Ranges in the response header to indicate whether to accept part of the data requested by a resource. But there seems to be a small trap here, that is, different servers may return different values to indicate that they can accept requests for some resources. It seems that the unified method is that when the server does not support requesting part of the data, it will return Accept-Ranges: none. We just need to determine whether the returned value is equal to none. The code is as follows:
Private static bool IsAcceptRanges (WebResponse res) {if (res.Headers ["Accept-Ranges"]! = null) {string s = res.Headers ["Accept-Ranges"]; if (s = = "none") {return false;}} return true;}
Check whether the server-side files have changed
When we download part of a file, we may download it immediately, or we may download it again after a period of time, or we may never download it again.
The question here is how to make sure that the file on the server is the same as the one that was downloaded the next time you want to continue downloading. If the file on the server has been updated, you need to download it from scratch anyway. Continued transmission of breakpoints makes sense only if the files on the server have not changed.
HTTP response headers provide us with different options for this problem. Both ETag and Last-Modified can complete the task.
Let's take a look at ETag:
The ETag response-header field provides the current value of the entity tag for the requested variant. (quoted from RFC2616 14.19 ETag)
To put it simply, an ETag is a string that identifies the content of the current request, and when the requested resource changes, the corresponding ETag will also change. Well, the easiest way is to save the ETag in the response header the first time you request it and compare it the next time you request it. The code is as follows:
String newEtag = GetEtag (response); / / tempFileName refers to part of the file content that has been downloaded locally / / tempFileInfoName refers to the temporary file if (File.Exists (tempFileName) & & File.Exists (tempFileInfoName)) {string oldEtag = File.ReadAllText (tempFileInfoName); if (! string.IsNullOrEmpty (oldEtag) & &! string.IsNullOrEmpty (newEtag) & & newEtag = = oldEtag) {/ / Etag remains unchanged and you can resume resumeDowload = true by breakpoint. }} else {if (! string.IsNullOrEmpty (newEtag)) {File.WriteAllText (tempFileInfoName, newEtag);}} private static string GetEtag (WebResponse res) {if (res.Headers ["ETag"]! = null) {return res.Headers ["ETag"];} return null;}
Let's take a look at Last-Modified:
The Last-Modified entity-header field indicates the date and time at which the origin server believes the variant was last modified. (quoted from RFC2616 14.29 Last-Modified)
Last-Modified is the last time the requested resource was modified on the server. It is used in much the same way as ETag.
I feel that we can achieve our goal by using either ETag or Last-Modified. But you can also use both to do double check, who knows whether the implementation of the web server strictly follows the HTTP protocol!
Save intermediate results
The main thing here is to use C # for file operation. The general idea is that if there are undownloaded files, add the newly downloaded bytes to the end of the file, no longer verbose, interested students please look directly at the demo code.
Verify the file
In the process of resuming upload at breakpoint, we download and merge files in units of byte. If there are any exceptions that have not been handled properly in the whole process, the final file may be different from the source file. Therefore, it is best to be able to verify the downloaded file. But this is also the most difficult and difficult to achieve. Because it needs server-side support, for example, the server-side provides a downloadable file as well as the MD5 hash of the file. Of course, if we create the server side ourselves, we can implement it. But how can we require existing web servers to provide such functionality?
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.