一.模擬Get請求(無參)
首先導入HttpClient依賴
1 <dependency> 2 <groupId>org.apache.httpcomponents</groupId> 3 <artifactId>httpclient</artifactId> 4 <version>4.5.2</version> 5 </dependency>
測試代碼
1 //Get請求 2 public static void getRequest() throws IOException { 3 //創建一個默認的鏈接 4 CloseableHttpClient client = HttpClients.createDefault(); 5 //創建一個請求 6 HttpGet httpGet=new HttpGet("https://www.cnblogs.com/chx9832"); 7 //執行請求獲取響應的結果 8 CloseableHttpResponse response = client.execute(httpGet); 9 //獲取響應的狀態碼 10 int statusCode = response.getStatusLine().getStatusCode(); 11 System.out.println("服務器返回狀態碼:"+statusCode); 12 //服務器正常響應 13 if(statusCode ==200){ 14 //獲取響應結果 15 System.out.println(EntityUtils.toString(response.getEntity(),"UTF-8")); 16 } 17 //關閉結果對象 18 response.close(); 19 //關閉鏈接 20 client.close(); 21 }
通過main方法調用就可拿到目標地址的代碼段

一.模擬Post請求(帶參)
與get請求類似,不同的是帶參需要創建參數隊列,並把參數封裝到請求體再set進請求中.
1 //模擬Post請求 2 public static void postRequest() throws IOException { 3 //創建鏈接 4 CloseableHttpClient client = HttpClients.createDefault(); 5 //創建請求 6 HttpPost httpPost=new HttpPost("http://localhost:8080/HttpClientServlet"); 7 8 //創建參數隊列 9 List<NameValuePair> pairList=new ArrayList<>(); 10 pairList.add(new BasicNameValuePair("username","懷鑫")); 11 //創建請求體,封裝參數 12 UrlEncodedFormEntity entity=new UrlEncodedFormEntity(pairList,"UTF-8"); 13 //將請求體交給當前請求 14 httpPost.setEntity(entity); 15 16 //發送請求,接收結果 17 CloseableHttpResponse response = client.execute(httpPost); 18 System.out.println("接收到的結果為:"+EntityUtils.toString(response.getEntity(),"UTF-8")); 19 20 21 //關閉資源 22 response.close(); 23 client.close(); 24 }
新建一個web項目,創建一個servlet模擬服務器,啟動tomcat等待請求
1 package com.chx; 2 3 import javax.servlet.ServletException; 4 import javax.servlet.annotation.WebServlet; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 import java.io.IOException; 9 10 @WebServlet("/HttpClientServlet") 11 public class HttpClientServlet extends HttpServlet { 12 @Override 13 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 14 //獲取數據 15 String username = req.getParameter("username"); 16 System.out.println("接收的數據:"+username); 17 resp.setContentType("text/html;charset=utf-8"); 18 resp.getWriter().write("服務器正確接收到數據~"); 19 } 20 21 @Override 22 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 23 doPost(req,resp); 24 } 25 }
回到post請求方法啟動后執行效果

服務器端

這樣,就完成了簡單的httpclient的get和post請求...
總結:
1. 創建HttpClient對象。
2. 創建請求方法的實例,並指定請求URL。如果需要發送GET請求,創建HttpGet對象;如果需要發送POST請求,創建HttpPost對象。
3. 如果需要發送請求參數,可調用HttpGet、HttpPost共同的setParams(HttpParams params)方法來添加請求參數;對於HttpPost對象而言,也可調用setEntity(HttpEntity entity) 方法來設置請求參數。
4. 調用HttpClient對象的execute(HttpUriRequest request)發送請求,該方法返回一個HttpResponse。
5. 調用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可獲取服務器的響應頭;調用HttpResponse的getEntity()方法可獲取HttpEntity對象,該對象包裝了服務 器的響應內容。程序可通過該對象獲取服務器的響應內容。
6. 釋放連接。無論執行方法是否成功,都必須釋放連接
