7

I'm using apache httpclient 4.4 to do https requests, and I can constantly to see org.apache.http.NoHttpResponseException.

Here's my code to create the httpclient.

   TrustManager[] trustAllCerts =
         new TrustManager[] { new X509TrustManager() {
            @Override
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
               return null;
            }

            @Override
            public void checkClientTrusted(X509Certificate[] certs,
                  String authType) {
            }

            @Override
            public void checkServerTrusted(X509Certificate[] certs,
                  String authType) {
            }

         } };
   SSLContext sslcontext = null;
   try {
      sslcontext = SSLContext.getInstance("SSL");
      sslcontext.init(null, trustAllCerts, new java.security.SecureRandom());
   } catch (NoSuchAlgorithmException e) {
      logger.warn(e.getMessage());
   } catch (KeyManagementException e) {
      logger.warn(e.getMessage());
   }
   // Allow TLSv1 protocol only
   final SSLConnectionSocketFactory sslsf =
         new SSLConnectionSocketFactory(sslcontext,
               new String[] { "TLSv1" }, null, NoopHostnameVerifier.INSTANCE);

   Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder
         .<ConnectionSocketFactory> create().register("https", sslsf)
         .build();
   PoolingHttpClientConnectionManager cm =
         new PoolingHttpClientConnectionManager(socketFactoryRegistry);

   // Increase max total connection to 200
   cm.setMaxTotal(200);
   // Increase default max connection per route to 20
   cm.setDefaultMaxPerRoute(20);

Before execute the request, I do cm.closeExpiredConnections(); and conMgr.closeIdleConnections(30, TimeUnit.SECONDS). I added this after I read the answer of this question

Anything more I need to do?

Community
  • 1
  • 1
Bomin
  • 1,619
  • 5
  • 24
  • 39

2 Answers2

8

Another way around this issue is to configure your org.apache.http.impl.client.AbstractHttpClient httpClient to never reuse connections:

httpClient.setReuseStrategy(new NoConnectionReuseStrategy());

The same can be configured on a org.apache.http.impl.client.HttpClientBuilder builder:

builder.setConnectionReuseStrategy(new NoConnectionReuseStrategy());
Arie Z.
  • 1,017
  • 8
  • 6
5

Finally figured it out on my own. If the previous response gives header, "connections=close", next request always gets this exception. So, when seeing this header, put 2 more lines

conManager.closeExpiredConnections();
conManager.closeIdleConnections(0, TimeUnit.SECONDS);

to let connection manager close the connection so that the connection won't be used by the next request.

Bomin
  • 1,619
  • 5
  • 24
  • 39
  • 4
    Isn't this will close all the connections regardless of statuses since you are setting the idle time to 0? – Fan Jin Nov 16 '18 at 00:09
  • My understanding is if the server closed the connection in previous response, next request should throw a `SocketException` when "writing". `NoHttpResponseException` is thrown when reading. – Devs love ZenUML Aug 28 '20 at 08:11