
Apache HttpClient - 關閉連線
如果您手動處理 HTTP 響應而不是使用響應處理器,則需要自行關閉所有 http 連線。本章說明如何手動關閉連線。
手動關閉 HTTP 連線時,請按照以下步驟操作:
步驟 1 - 建立 HttpClient 物件
HttpClients 類的 createDefault() 方法返回 CloseableHttpClient 類的物件,它是 HttpClient 介面的基本實現。
使用此方法,建立 HttpClient 物件,如下所示:
CloseableHttpClient httpClient = HttpClients.createDefault();
步驟 2 - 啟動 try-finally 塊
啟動 try-finally 塊,在 try 塊中編寫程式中的其餘程式碼,並在 finally 塊中關閉 CloseableHttpClient 物件。
CloseableHttpClient httpClient = HttpClients.createDefault(); try{ //Remaining code . . . . . . . . . . . . . . . }finally{ httpClient.close(); }
步驟 3 - 建立 HttpGetobject
HttpGet 類表示 HTTP GET 請求,它使用 URI 檢索給定伺服器的資訊。
透過例項化 HttpGet 類並傳遞表示 URI 的字串來建立 HTTP GET 請求。
HttpGet httpGet = new HttpGet("https://tutorialspoint.tw/");
步驟 4 - 執行 Get 請求
CloseableHttpClient 物件的 execute() 方法接受 HttpUriRequest(介面)物件(即 HttpGet、HttpPost、HttpPut、HttpHead 等)並返回響應物件。
使用給定方法執行請求:
HttpResponse httpResponse = httpclient.execute(httpGet);
步驟 5 - 啟動另一個(巢狀)try-finally
啟動另一個 try-finally 塊(巢狀在之前的 try-finally 中),在程式的此 try 塊中編寫其餘程式碼,並在 finally 塊中關閉 HttpResponse 物件。
CloseableHttpClient httpclient = HttpClients.createDefault(); try{ . . . . . . . . . . . . . . CloseableHttpResponse httpresponse = httpclient.execute(httpget); try{ . . . . . . . . . . . . . . }finally{ httpresponse.close(); } }finally{ httpclient.close(); }
示例
每當您建立/獲取請求、響應流等物件時,在下一行啟動 try finally 塊,在 try 中編寫其餘程式碼,並在 finally 塊中關閉相應的物件,如下面的程式所示:
import java.util.Scanner; 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 CloseConnectionExample { public static void main(String args[])throws Exception{ //Create an HttpClient object CloseableHttpClient httpclient = HttpClients.createDefault(); try{ //Create an HttpGet object HttpGet httpget = new HttpGet("https://tutorialspoint.tw/"); //Execute the Get request CloseableHttpResponse httpresponse = httpclient.execute(httpget); try{ Scanner sc = new Scanner(httpresponse.getEntity().getContent()); while(sc.hasNext()) { System.out.println(sc.nextLine()); } }finally{ httpresponse.close(); } }finally{ httpclient.close(); } } }
輸出
執行上述程式後,將生成以下輸出:
<!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"> <link href = "https://cdn.muicss.com/mui-0.9.39/extra/mui-rem.min.css" rel = "stylesheet" type = "text/css" /> <link 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>
廣告