The third article comes late. The previous article describes how to push information to the server. This article describes how to pull down information from the server quickly and accurately.
There are many large source files on the network, such as zip packages for people to download, movies (You know), so how can we download them quickly? The first reaction is to download multiple threads,
So how are these things done? First, we can pull a rar from the QQ Transfer Station.
Then we use fiddler to monitor and we will find an interesting phenomenon:
First: 7.62*1024*1024 ≈ 7990914 million is indeed this file
Second: I am clearly an http link. How does tmd become n or more? Interesting.
Okay. Let's continue and see what these links have done?
In the end, we found that there is a Conent-Range field in the http protocol, which can split the total size of our files, download them in parallel, and merge the files. We probably know that
The powerful C # Class Library provides AddRange to obtain the specified range of resources in Http.
Since the file is split, you must first know the file ContentLength. If you are familiar with the http protocol, when sending a header message, the server returns
The header information contains a lot of things. At this time, we know the general situation of downloading resources. This is a bit like "not moving, grain and grass first.
Var request = (HttpWebRequest) HttpWebRequest. Create (url );
Request. Method = "Head ";
Request. Timeout = 3000;
Var response = (HttpWebResponse) request. GetResponse ();
Var code = response. StatusCode;
If (code! = HttpStatusCode. OK)
{
Console. WriteLine ("invalid download resource! ");
Return;
}
Var total = response. ContentLength;
Here is a decision on whether to determine the number of threads Based on the download volume or the number of threads. Because our download depends on the current network speed, a better solution in this case is:
Using the latter, I saw Mr. Cang twice in the flash memory these days, so I decided to check the download speed without using threads and threads.
Image size (217.27KB)
View Code
The figure below shows that our resources are divided into n segments. In the case of 217.27KB, multi-thread acceleration is not very obvious. We can try a larger file. Here I will
Put a 13 m rarfile locally.
// Request file public static string url = "http: // localhost: 56933/1. rar ";
Now, the effect is quite obvious.
Transferred from:
Http://www.cnblogs.com/huangxincheng/archive/2012/05/20/2509715.html