The httpurlconnection of Java: keep-alive

Source: Internet
Author: User
Tags int size keep alive

HttpURLConnection's perverted thing: keep-alive

The JDK comes with the httpurlconnection default boot keep-alive, and the httpurlconnection will be reused in the pool after use. Related description:

What is does the current JDK does for keep-alive?

The JDK supports both http/1.1 and http/1.0 persistent connections.

When the application finishes reading the response body or is the application calls close () on the InputStream returned by Urlconnection.getinputstream (), the JDK ' s HTTP protocol handler would try to clean up the connection and if successful, Put the connection into a connection cache for reuse by the future HTTP requests.

The support for HTTP keep-alive are done transparently. However, it can be controlled by the system Properties http.keepalive, and Http.maxconnections, as well as by http/1.1 Specifi Ed request and response headers.

The system properties that control the behavior of keep-alive is:

Http.keepalive=<boolean>
Default:true

Indicates if keep alive (persistent) connections should be supported.

Http.maxconnections=<int>
Default:5

Indicates the maximum number of connections per destination to being kept alive at any given time

HTTP header that influences connection persistence is:

Connection:close

If the "Connection" header is specified with the value "close" in either the request or the response header fields, it ind Icates that the connection should isn't being considered ' persistent ' after the current request/response are complete.

The current implementation doesn ' t buffer the response body. Which means that the application have to finish reading the response body or call Close () to abandon the rest of the Respon SE body, in order for the connection to is reused. Furthermore, current implementation would not try block-reading when cleaning up the connection, meaning if the whole respo NSE body is not available, the connection would not be reused.

If the server does not support keep-alive, this default behavior can cause a lot of egg aches, for example:

Keep-alive is not enabled by default on the MogileFS server. When uploading multiple files using HttpURLConnection, due to keep-alive this reuse feature, the actually put request after the first file in a For loop dies! Until the socket Request timeout! This behavior is really sick!

The solution described above has been mentioned, tested and completed to be feasible:
(1) System.setproperty ("Http.keepalive", String.valueof (false));
(2) Conn.setrequestproperty ("Connection", "close");
Choose one based on your personal interests.

The following is an implementation of mogilefs uploading multiple files (in fact, HttpURLConnection put request):

public static string upload (string uri, int size, inputstream content) throws Dfsclientexception, Malformedurlexception, I oexception {

HttpURLConnection conn = null;
try {
conn = (httpurlconnection) new URL (URI). OpenConnection ();
System.out.println (Conn.hashcode ());
Conn.setfixedlengthstreamingmode (size);

Conn.setusecaches (FALSE);
Conn.setdoinput (TRUE);
Conn.setdooutput (TRUE);
Conn.setconnecttimeout (1000);
Conn.setreadtimeout (2000);
Conn.setrequestmethod ("PUT");
System.setproperty ("Http.keepalive", String.valueof (false));
Conn.setrequestproperty ("Connection", "close");

Conn.connect ();

System.out.println (Conn.usingproxy ());
OutputStream out = Conn.getoutputstream ();
for (int bt = 0; (BT = Content.read ())! =-1;) {
Out.write (BT);
}
Out.close ();

int code = Conn.getresponsecode ();
StringBuilder sb = new StringBuilder ();
InputStream in = Conn.geterrorstream ();
if (in = = null) {
in = Conn.getinputstream ();
}
if (in = null) {
BufferedReader reader = new BufferedReader (New InputStreamReader (Conn.getinputstream ()));
for (String line = null; (line = Reader.readline ()) = null;) {
Sb.append (line);
}
Reader.close ();
}

System.out.format ("resp[%d]%s", code, SB);
System.out.println (URI);
} finally {
IF (conn! = null) {
Conn.disconnect ();
}
}

Return Uri.replace ("/upload", "/download");
}

The httpurlconnection of Java: keep-alive

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.