The creation of an HTTP connection takes time. If we are going to get 4 files from one server and get each file to reestablish the connection to get the data, this will inevitably reduce the efficiency of the file download, because there is a part of the time spent on establishing the connection instead of actually transmitting the useful data, a shows:
If we can set up a connection, transfer all four files, and then close the connection, it will inevitably increase the efficiency of file download, because the cost of establishing the connection is saved many times, the persistent connection is such a connection, it works in the way B shows:
The problem with a simple persistent connection is that each time we have to wait for the response from the last request to initiate the next connection, the input stream is completely idle when the request is made, and the output stream is completely idle when the response is obtained. If we can send a request and receive a response, it will certainly make more use of this connection, and the persistent connection that can be used like this is called a pipelined connection, which works as shown in C:
The Java code given below is an example of getting multiple files from a server through a piped connection:
PackageSocketImportJava.io.BufferedReader;ImportJava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.io.OutputStreamWriter;ImportJava.io.PrintWriter;ImportJava.net.Socket;ImportJava.net.UnknownHostException;ImportJava.nio.file.Files;ImportJava.nio.file.Path;ImportJava.nio.file.Paths;ImportJava.nio.file.StandardOpenOption; Public class reusesocket { Public Static void Main(string[] args)throwsUnknownhostexception, IOException {String host ="Www.cnblogs.com";intPort = the; String Path ="/chenying99/p/3735282.html"; String path2 ="/everseeker/p/5462853.html"; String result = getbypipepipelinedconnection (host, port, Path, path2); WriteFile (Result,"D:/test/pipelined.html","Utf-8"); }/** * Get all files specified on host specified by paths on a pipelined connection * @param host hostname * @param Port port * @pa Ram paths The collection of file paths on hosts specified by the host * @return The results returned by the server * @throws unknownhostexception * @th Rows IOException * / Public StaticStringgetbypipepipelinedconnection(String host,intPort, String...paths)throwsUnknownhostexception, IOException {StringBuilder lines =NewStringBuilder ();Try(Socket socket =NewSocket (host, Port);//output streamPrintWriter PrintWriter =NewPrintWriter ((NewOutputStreamWriter (Socket.getoutputstream (),"Utf-8")));//input streamBufferedReader BufferedReader =NewBufferedReader (NewInputStreamReader (Socket.getinputstream (),"Utf-8")); ) { for(String path:paths) {String Requesthead = generaterequesthead (host, path); Printwriter.println (Requesthead); Printwriter.flush (); } String line =NULL; while(line = Bufferedreader.readline ())! =NULL) {lines.append (line); Lines.append (System.lineseparator ()); } }returnLines.tostring (); }/** * * @param host name * @param path to a file on the path host * @return get pat on host specified hosts H The requested header of the specified file * / Private StaticStringGeneraterequesthead(string host, String path) {StringBuffer Requestheader =NewStringBuffer (); Requestheader. Append ("GET"+ Path +"http/1.1\n"). Append ("accept:text/html, Application/xhtml+xml, */*\n"). Append ("accept-language:zh-cn,zh;q=0.8\n"). Append ("user-agent:mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; trident/6.0) \ n "). Append ("HOST:"+ Host +"\ n"). Append ("\ n");returnRequestheader.tostring (); }/** * Write the string into the file, the file already exists will overwrite the original file * @param Content String * @param pathName file path name * @para M charsetname Character set * / Private Static void WriteFile(string content, String pathName, String charsetname) {Path Path = Paths.get (PathName);Try{files.write (path, content.getbytes (CharsetName), standardopenoption.create); }Catch(IOException e) {E.printstacktrace (); } }}
For a more detailed understanding of plumbing connections, you can read the "HTTP Authoritative guide" section 4.6, which is also pictured in this article.
Download Web page Java with a piped connection