Httpclient 4.0.1 - How to Release Connection

HttpClient 4.0.1 - how to release connection?

To answer my own question: to release the connection (and any other resources associated with the request) you must close the InputStream returned by the HttpEntity:

InputStream is = entity.getContent();

.... process the input stream ....

is.close(); // releases all resources

From the docs

Releasing connection of HttpClient 4.1.x for sequential execution with one HttpClient instance

Man it must work, try this :

    HttpResponse response;
String responseBody;
try {
response = httpclient.execute(request);
int sc = response.getStatusLine().getStatusCode();
if (sc != HttpStatus.SC_OK)
throw new SystemException("google status code : " + sc);

HttpEntity entity = response.getEntity();
if (entity == null)
throw new SystemException("response entity null");
responseBody = EntityUtils.toString(entity);
} catch (Exception e) {
request.abort();
throw new SystemException("Problem when executing Google request", e);
}

Where is the documentation for HttpClient 4.1.x?

Fortunately, the Wayback Machine can get you the archived site:
https://web.archive.org/web/20120103180744/http://hc.apache.org/httpcomponents-client-ga/index.html

The (pretty clumsy) alternative would be to build it yourself:

  1. Check out HttpClient: svn co http://svn.apache.org/repos/asf/httpcomponents/httpclient/tags/4.1.3 httpclient
  2. Build HttpClient's site, skipping subprojects: mvn -pl . site (the subprojects contain missing dependencies)
  3. The build will still fail, but it will get far enough to generate HTML and PDF documentation in target/site, including a big tutorial PDF.

That worked for me, although it's a pretty clumsy process.

HttpClient: How to have only one connection to the server?

I implemented the suggestion of Robert Rowntree (sorry not sure to properly reference name) by replacing the beginning code with:

// Increase max total connection to 200 and increase default max connection per route to 20. 
// Configure total max or per route limits for persistent connections
// that can be kept in the pool or leased by the connection manager.
PoolingHttpClientConnectionManager oConnectionMgr = new PoolingHttpClientConnectionManager();
oConnectionMgr.setMaxTotal(200);
oConnectionMgr.setDefaultMaxPerRoute(20);
oConnectionMgr.setMaxPerRoute(new HttpRoute(new HttpHost("192.168.20.120", 8080)), 20);

RequestConfig defaultRequestConfig = RequestConfig.custom()
.setSocketTimeout(5000)
.setConnectTimeout(5000)
.setConnectionRequestTimeout(5000)
.setStaleConnectionCheckEnabled(true)
.build();

//HttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(defaultRequestConfig).build();
CloseableHttpClient oClientCloseable = HttpClientBuilder.create()
.setConnectionManager(oConnectionMgr)
.setDefaultRequestConfig(defaultRequestConfig)
.build();

I still saw the bunch of authenticates.

I contacted the vendor and shared with them the log using the modified version and my code was clean.

My test sample created a connection (to a remote server) followed by deleting the connection and repeating however many times. Their code dumps the authenticate message each time a connection creation request arrives.

I was pointed to what technically I already knew that the line that creates a new RESTful connection to the service is always "XXXXX connection allowed". There was one of those, two if you count my going to the browser based interface afterwards to make sure that all my links were gone.

Sadly, I am not sure that I can use the Apache client, so sad. Apache does not support message bodies inside a GET request. To the simple minded here (me, in this case), Apache does not allow:

GET http://www.example.com/whatevermethod:myport?arg1=data1&arg2=data2

Apache HttpClient --> HttpGet does not have a setEntities command. Research showed that as a POST request, but the service is the way that it is and will not change, so...

Using the new httpclient-4.0.1 jar with a project using commons-httpclient

No, you should not. HttpClient 3.x and Httpclient 4.x share no common dependencies beyond Commons Logging and Commons Codec and should be able to co-exist in the same class loader just fine.

Apache httpclient android dealing with connection refused

HttpHostConnectException is a type of IOException, you are not handling it properly. You should have something like below. You probably do not want to throw a Exception if status_code is not 200, instead return a meaningful error responseString

  private String getServerContentsJson()
throws IOException {
HttpClient httpclient = new DefaultHttpClient();

String responseString = null;
HttpResponse response = null;
try {
response = httpclient.execute(new HttpGet(url));

StatusLine statusline = response.getStatusLine();

if (statusline.getStatusCode() == HttpStatus.SC_OK) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
responseString = out.toString();
out.close();
return responseString;
} else {
responseString = statusline.getReasonPhrase();
}
} catch (IOException e) {
throw new IOException(e.getMessage(), e);
//OR:
//responseString = "Could not reach server, please try again";
} finally {
if (response != null) {
response.getEntity().consumeContent();
}
}
return responseString;
}


Related Topics



Leave a reply



Submit