版权声明:本文为博主原创文章,转载时请在文章最前方附上本文地址。 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