title: Apache httpclient connection takes too long
body: <p>I'm building a client that consumes a webservice.
  One thing I need to do is to send a POST request then, store the returned cookie.
  When I send it using a curl command, it takes about less than 2 seconds to get the response.
  This is the command I used:</p>
  
  <pre><code>curl -c cookies.txt --cert client_certificate -X POST -H "Content-Type: application/json" -d 'request_body' "https://my_target_web_service"
  </code></pre>
  
  <p>But, when I use Apache httpclient (4.3.1), it takes about 5 seconds.
  Following is the method:</p>
  
  <pre><code>@Test
  public void firstCall() throws Exception {
      final String url = "https://my_target_web_service";
      final String requestBody = readFile("json/request_body.json");
      Executor executor = executor();
      CookieStore cookieStore = new BasicCookieStore();
      executor.cookieStore(cookieStore);
  
      Request request = Request.Post(url).bodyString(requestBody, ContentType.APPLICATION_JSON);
      LOG.debug(request.toString());
  
      Response response = executor.execute(request);
      LOG.info(response.returnResponse().getStatusLine().toString());
  }
  
  private Executor executor() throws Exception {
      KeyStore clientStore = secretKeyStore();
      SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(clientStore, "secret_key".toCharArray()).useTLS().build();
      SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext,
              SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
      CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
      Executor executor = Executor.newInstance(httpclient);
      return executor;
  }
  </code></pre>
  
  <p>LOG output shows that most of the time is spent in establishing the connection to the webservice (notice the jump from 15:44:12,610 to 15:44:17,426):</p>
  
  <pre><code>DEBUG [2013-10-16 15:44:12,485] [main                          ] org.apache.http.client.protocol.RequestAddCookies: CookieSpec selected: best-match
  DEBUG [2013-10-16 15:44:12,500] [main                          ] org.apache.http.impl.conn.PoolingHttpClientConnectionManager: Connection request: [route: {s}-&gt;https://...:443][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]
  DEBUG [2013-10-16 15:44:12,517] [main                          ] org.apache.http.impl.conn.PoolingHttpClientConnectionManager: Connection leased: [id: 0][route: {s}-&gt;https://...:443][total kept alive: 0; route allocated: 1 of 2; total allocated: 1 of 20]
  DEBUG [2013-10-16 15:44:12,524] [main                          ] org.apache.http.impl.execchain.MainClientExec: Opening connection {s}-&gt;https://...:443
  DEBUG [2013-10-16 15:44:12,610] [main                          ] org.apache.http.conn.HttpClientConnectionManager: Connecting to ...:443
  DEBUG [2013-10-16 15:44:17,426] [main                          ] org.apache.http.impl.execchain.MainClientExec: Executing request POST / HTTP/1.1
  DEBUG [2013-10-16 15:44:17,427] [main                          ] org.apache.http.impl.execchain.MainClientExec: Target auth state: UNCHALLENGED
  DEBUG [2013-10-16 15:44:17,427] [main                          ] org.apache.http.impl.execchain.MainClientExec: Proxy auth state: UNCHALLENGED
  </code></pre>
  
  <p>Can anybody tell me what I am missing or doing wrong? Or, is this a limitation of httpclient and/or java?</p>
  
  <p>Thanks,</p>