版權聲明:本文爲博主原創文章,轉載時請在文章最前方附上本文地址。 https://blog.csdn.net/qq_35033270/article/details/80112085apache
超文本傳輸協議(HTTP,HyperText Transfer Protocol)是互聯網上應用最爲普遍的一種網絡協議。全部的WWW文件都必須遵照這個標準。而HttpClient是能夠支持http相關協議的工具包,它有以下功能:
1.實現了全部的http方法(GET,POST,PUT,HEAD 等)
2.支持自動轉向
3.支持 HTTPS 協議
4.支持代理服務器等
既然HttpClient使用這麼普遍,則本文講解下Spring Boot 中怎麼使用HttpClient.以下:
一.引入相關依賴json
<!-- http所需包 -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
</dependency>
<!-- /http所需包 -->服務器
<!-- 數據解析所需包 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.4</version>
</dependency>
<!-- /數據解析所需包 -->
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
二.編寫相關工具類
寫個http的工具類,以便業務代碼直接調用,以下:網絡
/**
* Http工具類
*/
public class HttpUtils {session
/**
* 發送POST請求
* @param url 請求url
* @param data 請求數據
* @return 結果
*/
@SuppressWarnings("deprecation")
public static String doPost(String url, String data) {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(10000).setConnectTimeout(20000)
.setConnectionRequestTimeout(10000).build();
httpPost.setConfig(requestConfig);
String context = StringUtils.EMPTY;
if (!StringUtils.isEmpty(data)) {
StringEntity body = new StringEntity(data, "utf-8");
httpPost.setEntity(body);
}
// 設置回調接口接收的消息頭
httpPost.addHeader("Content-Type", "application/json");
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
context = EntityUtils.toString(entity, HTTP.UTF_8);
} catch (Exception e) {
e.getStackTrace();
} finally {
try {
response.close();
httpPost.abort();
httpClient.close();
} catch (Exception e) {
e.getStackTrace();
}
}
return context;
}app
/**
* 解析出url參數中的鍵值對
* @param url url參數
* @return 鍵值對
*/
public static Map<String, String> getRequestParam(String url) {工具
Map<String, String> map = new HashMap<String, String>();
String[] arrSplit = null;ui
// 每一個鍵值爲一組
arrSplit = url.split("[&]");
for (String strSplit : arrSplit) {
String[] arrSplitEqual = null;
arrSplitEqual = strSplit.split("[=]");url
// 解析出鍵值
if (arrSplitEqual.length > 1) {
// 正確解析
map.put(arrSplitEqual[0], arrSplitEqual[1]);
} else {
if (arrSplitEqual[0] != "") {
map.put(arrSplitEqual[0], "");
}
}
}
return map;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
三.業務代碼中使用
業務中代碼使用,拼裝請求Url和請求數據,就能夠調用工具類裏的doPost()方法開始直接使用咯。以下:.net
private String getFileStorePath(String courtId, String seesionId){ String fileStorePath = StringUtils.EMPTY; //請求參數 String data = "{\"courtId\":\"" + courtId + "\",\"sessionId\":\"" + seesionId + "\"}"; String fileServiceUrl="http://111.11.11.11:8086"; //發送請求,獲取結果 String result = HttpUtils.doPost(fileServiceUrl + "/ms-service/voice/search", data); if(StringUtils.isNotBlank(result)){ com.alibaba.fastjson.JSONObject jsonobject = JSON.parseObject(result); fileStorePath = jsonobject.getString("path"); logger.info("fileStorePath = " + fileStorePath); } return fileStorePath; }1234567891011121314若有不當之處,煩請斧正,一塊兒溝通。原文:https://blog.csdn.net/qq_35033270/article/details/80112085