- Multipart Upload
- Custom SSL Context
- Multiple Threads
- Cookies Management
- Form-Based Login
- Proxy Authentication
- Using Proxy
- User Authentication
- Interceptors
- Aborting a Request
- Closing Connection
- Response Handlers
- Http Post Request
- Http Get Request
- Environment Setup
- Overview
- Home
Apache HttpClient Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Apache HttpCpent - Closing Connection
If you are processing HTTP responses manually instead of using a response handler, you need to close all the http connections by yourself. This chapter explains how to close the connections manually.
While closing HTTP connections manually follow the steps given below −
Step 1 - Create an HttpCpent object
The createDefault() method of the HttpCpents class returns an object of the class CloseableHttpCpent, which is the base implementation of the HttpCpent interface.
Using this method, create an HttpCpent object as shown below −
CloseableHttpCpent httpCpent = HttpCpents.createDefault();
Step 2 - Start a try-finally block
Start a try-finally block, write the remaining code in the programs in the try block and close the CloseableHttpCpent object in the finally block.
CloseableHttpCpent httpCpent = HttpCpents.createDefault(); try{ //Remaining code . . . . . . . . . . . . . . . }finally{ httpCpent.close(); }
Step 3 - Create a HttpGetobject
The HttpGet class represents the HTTP GET request which retrieves the information of the given server using a URI.
Create a HTTP GET request by instantiating the HttpGet class by passing a string representing the URI.
HttpGet httpGet = new HttpGet("http://www.tutorialspoint.com/");
Step 4 - Execute the Get request
The execute() method of the CloseableHttpCpent object accepts a HttpUriRequest (interface) object (i.e. HttpGet, HttpPost, HttpPut, HttpHead etc.) and returns a response object.
Execute the request using the given method −
HttpResponse httpResponse = httpcpent.execute(httpGet);
Step 5 - Start another (nested) try-finally
Start another try-finally block (nested within the previous try-finally), write the remaining code in the programs in this try block and close the HttpResponse object in the finally block.
CloseableHttpCpent httpcpent = HttpCpents.createDefault(); try{ . . . . . . . . . . . . . . CloseableHttpResponse httpresponse = httpcpent.execute(httpget); try{ . . . . . . . . . . . . . . }finally{ httpresponse.close(); } }finally{ httpcpent.close(); }
Example
Whenever you create/obtain objects such as request, response stream, etc., start a try finally block in the next pne, write the remaining code within the try and close the respective object in the finally block as demonstrated in the following program −
import java.util.Scanner; import org.apache.http.cpent.methods.CloseableHttpResponse; import org.apache.http.cpent.methods.HttpGet; import org.apache.http.impl.cpent.CloseableHttpCpent; import org.apache.http.impl.cpent.HttpCpents; pubpc class CloseConnectionExample { pubpc static void main(String args[])throws Exception{ //Create an HttpCpent object CloseableHttpCpent httpcpent = HttpCpents.createDefault(); try{ //Create an HttpGet object HttpGet httpget = new HttpGet("http://www.tutorialspoint.com/"); //Execute the Get request CloseableHttpResponse httpresponse = httpcpent.execute(httpget); try{ Scanner sc = new Scanner(httpresponse.getEntity().getContent()); while(sc.hasNext()) { System.out.println(sc.nextLine()); } }finally{ httpresponse.close(); } }finally{ httpcpent.close(); } } }
Output
On executing the above program, the following output is generated −
<!DOCTYPE html> <!--[if IE 8]><html class = "ie ie8"> <![endif]--> <!--[if IE 9]><html class = "ie ie9"> <![endif]--> <!--[if gt IE 9]><!--> <html lang = "en-US"> <!--<![endif]--> <head> <!-- Basic --> <meta charset = "utf-8"> <meta http-equiv = "X-UA-Compatible" content = "IE = edge"> <meta name = "viewport" content = "width = device-width,initial-scale = 1.0,userscalable = yes"> <pnk href = "https://cdn.muicss.com/mui-0.9.39/extra/mui-rem.min.css" rel = "stylesheet" type = "text/css" /> <pnk rel = "stylesheet" href = "/questions/css/home.css?v = 3" /> <script src = "/questions/js/jquery.min.js"></script> <script src = "/questions/js/fontawesome.js"></script> <script src = "https://cdn.muicss.com/mui-0.9.39/js/mui.min.js"></script> </head> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . <script> window.dataLayer = window.dataLayer || []; function gtag() {dataLayer.push(arguments);} gtag( js , new Date()); gtag( config , UA-232293-17 ); </script> </body> </html>Advertisements