2

I'm using URLConnection for my http client. Using Wireshark, I can see that setting up an https connection can take up to four seconds. Therefore I would like to pool connections if possible to avoid the https setup time. My flow looks like this:

public String work(String url) {
    HttpURLConnection conn = (HttpURLConnection)(new URL(url)).openConnection();
    BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String response = br.readResponseFromConnection();
    conn.disconnect();
    br.close();
    return response;
}

public void onBtnClickTest() {
    work("https://example.com/echo?param=abc");
}

With the above setup, I click my test button twice, but it looks like the https setup is done for each call, which makes me think the connection isn't really being reused (at least not in the way that I was hoping). Through Wireshark I see something like the following :

// first click
Client Hello
Server Hello 
Certificate, Server Hello Done
Client Key Exchange, Change Cipher Spec, Encrypted...
Encrypted Handshake Message...
Application Data

// second click, 20 seconds after my first click.
Client Hello
Server Hello 
Certificate, Server Hello Done
Client Key Exchange, Change Cipher Spec, Encrypted...
Encrypted Handshake Message...
Application Data

However, if I click the test button twice within a span of < 5 seconds, it looks like the handshake is skipped for the second run, and I immediately see the "Application Data" message. I think I remember reading somewhere that URLConnection only keeps connections pooled for 5 seconds.

So my questions:

  1. Can URLConnection actually pool my connections in the way that I want, in that subsequent connections can skip the https handshake (if hitting the same domain)?
  2. If the above is possible, is there a way to increase the duration that connections stay pooled? My application is unlikely to make http calls within 5 seconds of eachother.
  3. I know HttpClient offers a pool manager to reuse a connection, but looks more complicated than URLConnection. Can it get me around # 1 & 2 if they won't do what I need?

Thank you

user291701
  • 38,411
  • 72
  • 187
  • 285
  • Possible duplicate of [How does URLConnection in java reuses the connection from the pool](https://stackoverflow.com/questions/8266488/how-does-urlconnection-in-java-reuses-the-connection-from-the-pool) – rogerdpack Nov 01 '17 at 21:57

1 Answers1

0

You can use Keep-Alive header property to persisit any httpconnection. Here is documentation on Http Keep-alive Http Keep Alive

section What can you do to help with Keep-Alive gives exact information what you are looking for. Quick search in SO gave me another link which is exactly like your question.

SO disucssion link

Community
  • 1
  • 1
kosa
  • 65,990
  • 13
  • 130
  • 167
  • Hi, the HttpURLConnection docs say that Keep-Alive is on by default. The doc you linked to does not mention how long connections are pooled for (I believe this may vary between implementations). Is there any way to control that duration? Thank you – user291701 Jan 05 '12 at 14:48
  • This is the doc for HttpURLConnection on android that I'm referring to: http://developer.android.com/reference/java/net/HttpURLConnection.html – user291701 Jan 05 '12 at 14:50
  • Also this is where I read about the connections only being pooled for 5 seconds: http://www.java.net/node/656952 "Otherwise, the timeout is only 5 seconds, after which the socket will not be reused ". I wonder if HttpClient has the same timeout limitation. It seems you can't change that in HttpURLConnection unfortunately unless the server returns back a timeout value for it to honor? – user291701 Jan 05 '12 at 14:54
  • I don't think there is a way you can control the Keep-Alive. My guess is, if more than 5 connections, connection with highest last used time might be taken out. I doubt 5 seconds comment, couldn't find any official documentation with that time limit. – kosa Jan 05 '12 at 15:23
  • Agreed I can't find any documentation either, just trial and error with Wireshark. If I reconnect in < 5 seconds, I see the https handshake setup stuff is avoided. If > 5 seconds, the whole thing is set up again. – user291701 Jan 05 '12 at 17:05