While Using Apache HttpClient library to make Http requests, we should always release connection immediately after completion of the operation. For making HttpRequest Apache HttpClient library provides CloseableHttpClient and CloseableHttpResponse classes which implements java closable interface.
By calling the close method of a closeable object in finally block we can close or dispose of connection cleanly.
public class ClientConnectionReleaseExample { public final static void main(String[] args) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); try { HttpGet httpget = new HttpGet("https://google.co.in"); System.out.println("Executing request " + httpget.getRequestLine()); CloseableHttpResponse response = httpclient.execute(httpget); try { System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); // Get hold of the response entity HttpEntity entity = response.getEntity(); // If the response does not enclose an entity, there is no need // to bother about connection release if (entity != null) { InputStream inStream = entity.getContent(); try { inStream.read(); // do something useful with the response } catch (IOException ex) { // In case of an IOException the connection will be released // back to the connection manager automatically throw ex; } finally { // Closing the input stream will trigger connection release inStream.close(); } } } finally { response.close(); } } finally { httpclient.close(); } } }
For Java 8 and above we can use new try syntax, in this compiler automatically adds finally block and call objects close methods. Note new try block can be used with object implements Closeable interface.
package org.httpclient.example; import java.io.IOException; import java.io.InputStream; import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; public class ClientConnectionReleaseExample { public final static void main(String[] args) throws Exception { try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpGet httpget = new HttpGet("https://google.co.in"); System.out.println("Executing request " + httpget.getRequestLine()); try (CloseableHttpResponse response = httpclient.execute(httpget)) { System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); // Get hold of the response entity HttpEntity entity = response.getEntity(); // If the response does not enclose an entity, there is no need // to bother about connection release if (entity != null) { try (InputStream inStream = entity.getContent()) { inStream.read(); // do something useful with the response } catch (IOException ex) { // In case of an IOException the connection will be released // back to the connection manager automatically throw ex; } } } } } }
2 responses to “Manually Closing connection of Apache HttpClient in Java”
What if httpClient connection is closed ? Will it not throw exception Socket is closed ?
One should close the connection after the completion of the reading response.