This article supporting source code
Overview
In today's network age, downloading software is one of the most frequently used software. Over the years, download technology is also constantly developing. The original download is simply a "download" process that reads files sequentially from a Web server. The biggest problem is that because of the instability of the network, once the disconnect makes the download process interrupted, you have to start all over again.
Then, the concept of "break-through" comes out, as the name suggests, if the download is interrupted, after the connection is reconnected, skip the part that has already been downloaded, and download only the part that has not been downloaded yet.
Whether "multi-threaded download" technology is the invention of Mr. Hong Yijong, it is an indisputable fact that Hong has given the technology unprecedented attention. In the "Network Ant" Software Popular, many download software also have to follow, whether with? quot, multi-threaded download technology, and even how many download threads can support the people to evaluate the download software elements. Multi-threaded downloads are based on a Web server that supports remote random reads, or "breakpoint continuation". This way, when downloading, you can split the file into sections, and each part creates a download thread to download.
Now, do not say to write a special download software, in their own written software, the addition of the download function is sometimes very necessary. It is useful and practical to have your software support automatic online upgrades, or to automatically download new data for data updates in software. The topic of this article is how to write a download module that supports "Breakpoint continuation" and "multithreading". Of course, the download process is very complex, in an article is difficult to clarify all, so, and the download process is not directly related to the part of the basic ignored, such as exception handling and network error handling, please readers attention. The development environment I use is C + + Builder 5.0, and friends who use other development environments or programming languages should make the appropriate changes themselves.
Introduction to HTTP protocol
The download files are the process by which the computer interacts with the Web server, and the professional name of the "language" that they interact with is the protocol. There are a number of protocols for transferring files, the most commonly used are HTTP (Hypertext Transfer Protocol) and FTP (FTP), and I am using HTTP.
The most basic commands for the HTTP protocol are only three: get, post, and head. Get requests a particular object from the Web server, such as an HTML page or a file, that the Web server sends this object as a response through a socket connection; The head command gives the server a basic description of the object, such as the type, size, and update time of the object. The post command is used to send data to the Web server, typically sending the information to a separate application, which is returned to the browser by processing the generated dynamic results. The download is implemented by the GET command.
The basic download process
Write the download program, you can use the socket function directly, but this requires developers understand, familiar with the TCP/IP protocol. To simplify the development of Internet client software, Windows provides a set of WinInet APIs that encapsulate commonly used network protocols and greatly reduce the threshold for developing Internet software. We need to use the WinInet API function as shown in Figure 1, the call order is basically from top to bottom, its specific function prototype please refer to MSDN.
Figure 1
When using these functions, you must strictly distinguish between the handles they use. The types of these handles are the same, they are all hinternet, but the effects are different, which is very confusing. The order in which these handles are generated and the invocation relationships can be divided into three levels, and the next level of the handle is obtained by the upper-level handle.
InternetOpen is the first function to call, it returns the highest level of hinternet handle, I am accustomed to defined as Hsession, the session handle.
InternetConnect uses the hsession handle, which returns the HTTP connection handle, which I define as hconnect.
HttpOpenRequest using the Hconnect handle, the returned handle is the HTTP request handle, defined as hrequest.
HttpSendRequest, HttpQueryInfo, Internetsetfilepointer, and InternetReadFile use the handles returned by HttpOpenRequest, that is, hrequest.
When these handles are no longer used, you should use the function InternetCloseHandle to turn it off to free up the resources it occupies.
First create a thread module named Thttpgetthread, automatically suspend after creation, and I want the thread to be destroyed automatically after completion, so set in the constructor:
Freeonterminate = True; Automatically delete
and add the following member variables:
char Buffer[HTTPGET_BUFFER_MAX+4]; // 数据缓冲区
AnsiString FURL; // 下载对象的URL
AnsiString FOutFileName; // 保存的路径和名称
HINTERNET FhSession; // 会话句柄
HINTERNET FhConnect; // http连接句柄
HINTERNET FhRequest; // http请求句柄
bool FSuccess; // 下载是否成功
int iFileHandle; // 输出文件的句柄