"Network Ants", Falshget and many other multi-threaded download software is a necessary tool for users, the use of these tools can quickly download the larger files from the server, these tools are working characteristics of the server side of the file into several segments, each segment, at the same time to download. To write such a program, first of all, must have a better understanding of the HTTP protocol; Second, the effective use of multithreaded programming means in software implementation.
Introduction to the HTTP protocol
HTTP protocol is a Hypertext Transfer Protocol (hypertext Transfer Protocol), working on the network application layer, since 1990 has been widely used in WWW global information Services, the detailed description of the HTTP protocol can be read online RFC2518, RFC2616 and other documents.
HTTP protocol old Standard is http/1.0, the current most common standard is http/1.1. http/1.1 is upgraded on http/1.0 basis, adding some functionality to fully compatible http/1.0. http/1.0 does not support file breakpoint continuation, if the server using http/1.0, "network ants" any multithreaded download program can only be downloaded on a single thread, fortunately, the majority of the current Web server has adopted a http/1.1, so, the following will be based on http/1.1 introduction.
Important commands related to HTTP protocol
Browsing Web pages and downloading files based on HTTP browsers work like client/server mode: The browser sends an HTTP request line to the Web server, and the Web server returns a status row or multiple response headers, a blank row, and related documents when a valid request is received. Based on this principle, the download program must implement the ability to send requests to the server and get the server response status.
1. Send a GET request command to the server
An HTTP request consists of a request line, an optional number of request headers, a blank line, and some additional data in the post case. The format of the request line is:
Request Method URI http/version number
The Get command is a commonly used document request method in browsers, used in the middle of a program
Get URI http/1.1
Send the request line (line number 3) to the Web server with the following Java code:
....
clientSocket = new Socket(host, port);//打开要下载文件服务器的Socket
outStream = new PrintStream(clientSocket.getOutputStream());
....
outStream.println(“GET”+uri+“ HTTP/1.1”);
outStream.println(“Host:”+host);
outStream.println(“Accept:*/* ”);
outStream.println(“Referer:”);
outStream.println();
....
Note: Line 4th gives the host name and port number in the URL, and line 5th indicates that the client receives all MIME types, and the 7th line sends a blank line indicating that the request line ends.