使用HttpClient發送請求的一般步驟如下:
(1) 創建HttpClient對象。
(2) 創建請求方法的實例,並指定請求URL。如果需要發送GET請求,創建HttpGet對象;如果需要發送POST請求,創建HttpPost對象。
(3) 如果需要發送請求參數,可調用HttpGet對象的setParams(HetpParams params)方法來添加請求參數;對於HttpPost對象而言,可調用setEntity(HttpEntity entity)方法來設置請求參數。
(4) 調用HttpClient對象的execute(HttpUriRequest request)發送請求,該方法返回一個HttpResponse。
(5) 調用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可獲取服務器的響應頭;調用HttpResponse的getEntity()方法可獲取HttpEntity對象,該對象包裝了服務器的響應內容。程序可通過該對象獲取服務器的響應內容。
(6) 釋放連接。無論執行方法是否成功,都必須釋放連接。
有兩種依賴包,commons和apache依賴包,選擇其中一種即可。以下是混用的,僅做參考即可。
引入工具依賴包如下:
<!-- commons-httpclient http請求工具依賴包 -->
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<!-- apache http請求工具依賴包 -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.6</version>
</dependency>
方法如下所示:
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.lang.ArrayUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.*;
@Slf4j
public class HttpClientTest {
/**
* 發起post請求 沒有任何body參數的情況
*
* @param url 請求的目標url地址
* @param data 請求數據
* @return 將響應結果轉換成string返回
* @throws IOException 可能出現的異常
*/
private static String postMsg(String url, String data) throws IOException {
// 根據url地址發起post請求
HttpPost httppost = new HttpPost(url);
StringEntity stEntity;
// 獲取到httpclient客戶端
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
// 設置請求的一些頭部信息
httppost.addHeader("Content-Type", "application/json");
httppost.addHeader("procode", "test");
stEntity = new StringEntity(data, "UTF-8");
httppost.setEntity(stEntity);
// 設置請求的一些配置設置,主要設置請求超時,連接超時等參數
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(5000).setConnectionRequestTimeout(1000).setSocketTimeout(5000)
.build();
httppost.setConfig(requestConfig);
// 執行請求
CloseableHttpResponse response = httpclient.execute(httppost);
// 請求結果
String resultString = "";
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
log.info("請求狀態:{}", response.getStatusLine().getStatusCode());
// 獲取請求響應結果
HttpEntity entity = response.getEntity();
if (entity != null) {
// 將響應內容轉換為指定編碼的字符串
resultString = EntityUtils.toString(entity, "UTF-8");
log.info("Response content:{}", resultString);
return resultString;
}
} else {
log.info("請求失敗!");
return resultString;
}
} catch (Exception e) {
throw e;
} finally {
httpclient.close();
}
return null;
}
/**
* body有內容的post請求方法 controller可以用bean直接接收參數
*
* @param url 目標地址
* @param params 封裝的參數
* @param codePage 字符編碼
* @return 將響應結果轉換成string返回
* @throws Exception 可能出現的異常
*/
private synchronized static String postData(String url, Map<String, String> params, String codePage) throws Exception {
HttpClient httpClient = new HttpClient();
// 請求相關超時時間設置
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(10 * 1000);
httpClient.getHttpConnectionManager().getParams().setSoTimeout(10 * 1000);
PostMethod method = new PostMethod(url);
if (params != null) {
// 內容編碼設置
method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, codePage);
// 請求體信息設置
method.setRequestBody(assembleRequestParams(params));
// 請求頭設置
method.setRequestHeader("procode", "test");
}
// 響應結果信息處理
String result = "";
try {
httpClient.executeMethod(method);
result = new String(method.getResponseBody(), codePage);
} catch (Exception e) {
throw e;
} finally {
// 釋放連接
method.releaseConnection();
}
return result;
}
/**
* 組裝http請求參數
*
* @param data 鍵值對
* @return 返回一個名稱-值的鍵值對
*/
private synchronized static NameValuePair[] assembleRequestParams(Map<String, String> data) {
List<NameValuePair> nameValueList = new ArrayList<>();
Iterator<Map.Entry<String, String>> it = data.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> entry = it.next();
nameValueList.add(new NameValuePair(entry.getKey(), entry.getValue()));
}
log.info("鍵值對參數:{}", ArrayUtils.toString(nameValueList.toArray(new NameValuePair[nameValueList.size()])));
return nameValueList.toArray(new NameValuePair[nameValueList.size()]);
}
/**
* get請求
*
* @param url 目標請求地址
* @return 將響應結果轉換成string返回
*/
private static String get(String url) {
String result = "";
try {
// 根據地址獲取請求
HttpGet request = new HttpGet(url);
request.setHeader("procode", "test");
// 獲取當前客戶端對
CloseableHttpClient httpclient = HttpClients.createDefault();
// 通過請求對象獲取響應對象
HttpResponse response = httpclient.execute(request);
// 判斷請求結果狀態碼
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
result = EntityUtils.toString(response.getEntity());
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static void main(String[] args) throws Exception {
String url = "http://localhost:8080/sales-webapp/admin/report/smslist";
// 發起post請求
String resultPost = postMsg(url, "1");
log.info("post返回結果:{}", resultPost);
// 發起get請求
String resultGet = get(url);
log.info("get返回結果:{}", resultGet);
// 測試body有參數的post方法
Map<String, String> map = new HashMap<>(16);
map.put("age", "13");
map.put("name", "jason");
String resultPostHaveBody = postData(url, map, "UTF-8");
log.info("post帶有body請求返回結果:{}", resultPostHaveBody);
}
}
參考博文:
(1) https://blog.csdn.net/q394895302/article/details/90256482 (講的清晰明了,分開講述兩種依賴包發get和post請求)
(2) https://blog.csdn.net/u014429653/article/details/106985970 (代碼清晰)