使用流程
- 創建
HttpClient
對象 - 創建請求方法的實例,並指定請求 URL。如果需要發送 GET 請求,創建
HttpGet
對象;如果需要發送 POST 請求,創建HttpPost
對象。 - 如果需要發送請求參數,可調用
HttpGet
、HttpPost
共同的setParams(HttpParams params)
方法來添加請求參數;對於HttpPost
對象而言,也可調用setEntity(HttpEntity entity)
方法來設置請求參數。 - 調用
HttpClient
對象的execute(HttpUriRequest request)
發送請求,該方法返回一個HttpResponse
。 - 調用
HttpResponse
的getAllHeaders()
、getHeaders(String name)
等方法可獲取服務器的響應頭;調用HttpResponse
的getEntity()
方法可獲取HttpEntity
對象,該對象包裝了服務器的響應內容。程序可通過該對象獲取服務器的響應內容。 - 釋放連接。無論執行方法是否成功,都必須釋放連接
案例
pom.xml
配置如下:<!-- Apache Http Begin --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.5</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>fluent-hc</artifactId> <version>4.5.5</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpmime</artifactId> <version>4.5.5</version> </dependency> <!-- Apache Http End -->
-
創建 HttpGet 請求
package com.funtl.hello.httpclient; 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; import org.apache.http.util.EntityUtils; import java.io.IOException; public class MyTest { public static void main(String[] args) { get(); } private static void get() { // 創建 HttpClient 客戶端 CloseableHttpClient httpClient = HttpClients.createDefault(); // 創建 HttpGet 請求 HttpGet httpGet = new HttpGet("http://localhost:8080/content/page?draw=1&start=0&length=10"); // 設置長連接 httpGet.setHeader("Connection", "keep-alive"); // 設置代理(模擬瀏覽器版本) httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"); // 設置 Cookie httpGet.setHeader("Cookie", "UM_distinctid=16442706a09352-0376059833914f-3c604504-1fa400-16442706a0b345; CNZZDATA1262458286=1603637673-1530123020-%7C1530123020; JSESSIONID=805587506F1594AE02DC45845A7216A4"); CloseableHttpResponse httpResponse = null; try { // 請求並獲得響應結果 httpResponse = httpClient.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); // 輸出請求結果 System.out.println(EntityUtils.toString(httpEntity)); } catch (IOException e) { e.printStackTrace(); } // 無論如何必須關閉連接 finally { if (httpResponse != null) { try { httpResponse.close(); } catch (IOException e) { e.printStackTrace(); } } if (httpClient != null) { try { httpClient.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
-
創建 HttpPost 請求
package com.funtl.hello.httpclient; import org.apache.http.HttpEntity; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; public class MyTest { public static void main(String[] args) { post(); } private static void post() { // 創建 HttpClient 客戶端 CloseableHttpClient httpClient = HttpClients.createDefault(); // 創建 HttpPost 請求 HttpPost httpPost = new HttpPost("http://localhost:8080/content/page"); // 設置長連接 httpPost.setHeader("Connection", "keep-alive"); // 設置代理(模擬瀏覽器版本) httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"); // 設置 Cookie httpPost.setHeader("Cookie", "UM_distinctid=16442706a09352-0376059833914f-3c604504-1fa400-16442706a0b345; CNZZDATA1262458286=1603637673-1530123020-%7C1530123020; JSESSIONID=805587506F1594AE02DC45845A7216A4"); // 創建 HttpPost 參數 List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>(); params.add(new BasicNameValuePair("draw", "1")); params.add(new BasicNameValuePair("start", "0")); params.add(new BasicNameValuePair("length", "10")); CloseableHttpResponse httpResponse = null; try { // 設置 HttpPost 參數 httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); // 輸出請求結果 System.out.println(EntityUtils.toString(httpEntity)); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // 無論如何必須關閉連接 finally { try { if (httpResponse != null) { httpResponse.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (httpClient != null) { httpClient.close(); } } catch (IOException e) { e.printStackTrace(); } } } }