【二三方】_http请求发送工具类

http请求发送工具类

httpclient连接池

为了节约资源、提高效率,http连接客户端对象的创建采用连接池的形式:

1
2
3
4
5
6
7
8
9
10
11
private static HttpClient client = null;

static {
//创建连接池对象
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
//设置连接池大小
cm.setMaxTotal(128);
cm.setDefaultMaxPerRoute(128);
//从连接池中获取http连接客户端对象
client = HttpClients.custom().setConnectionManager(cm).build();
}
http请求的主要格式
  • get方法
  • post方法发送String类型form表单数据
  • post方法发送json数据
  • post方法发送Object类型form表单数据
引入依赖

需要引入org.apache.httpcomponents:httpclient和org.apache.httpcomponents:httpmime这两个依赖包,第一个依赖包用于提供http连接客户端,第二个依赖包用于支持multipart文件类型的post请求参数:

1
2
3
4
5
6
7
8
9
10
11
12
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.13</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
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.util.*;

public class HttpUtil {

private static HttpClient client = null;

static {
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(128);
cm.setDefaultMaxPerRoute(128);
client = HttpClients.custom().setConnectionManager(cm).build();
}

/**
* get方法
*/
public static String getRequest(String url, Map<String, String> headers, Map<String, String> param) throws Exception {
String result = null;
List<String> tmp = new ArrayList<>();
for (Map.Entry<String, String> entry : param.entrySet()) {
tmp.add(entry.getKey() + "=" + entry.getValue());
}
HttpGet httpGet = new HttpGet(url + "?" + String.join("&", tmp));

// 设置Header
if (headers != null && !headers.isEmpty()) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpGet.setHeader(new BasicHeader(entry.getKey(), entry.getValue()));
}
}

HttpResponse response = client.execute(httpGet);
if (response != null) {
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
result = EntityUtils.toString(resEntity);
}
}

return result;
}

/**
* post方法发送String类型form表单数据
*/
public static String postRequest(String url, Map<String, String> headers, Map<String, String> data) throws Exception {
String result = null;
HttpPost httpPost = new HttpPost(url);

// 设置Header
if (headers != null && !headers.isEmpty()) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpPost.setHeader(new BasicHeader(entry.getKey(), entry.getValue()));
}
}

// 设置参数
List<NameValuePair> list = new LinkedList<>();
for (Map.Entry<String, String> entry : data.entrySet()) {
list.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
if (list.size() > 0) {
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8");
httpPost.setEntity(entity);
}

HttpResponse response = client.execute(httpPost);
if (response != null) {
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
result = EntityUtils.toString(resEntity);
}
}

return result;
}

/**
* post方法发送json数据
*/
public static String postRequest(String url, String body, Map<String, String> header) throws Exception {
String result = null;
HttpPost httpPost = new HttpPost(url);

// 设置Header
if (header != null && !header.isEmpty()) {
for (Iterator<Map.Entry<String, String>> it = header.entrySet().iterator(); it.hasNext(); ) {
Map.Entry<String, String> entry = it.next();
httpPost.setHeader(new BasicHeader(entry.getKey(), entry.getValue()));
}
}

// 设置参数
httpPost.setEntity(new StringEntity(body, "UTF-8"));
HttpResponse response = client.execute(httpPost);
if (response != null) {
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
result = EntityUtils.toString(resEntity);
}
}
return result;
}

/**
* post方法发送Object类型form表单数据
*/
public static String postMultiRequest(String url, Map<String, String> headers, Map<String, Object> data) throws Exception {
String result = null;
HttpPost httpPost = new HttpPost(url);

// 设置Header
if (headers != null && !headers.isEmpty()) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpPost.setHeader(new BasicHeader(entry.getKey(), entry.getValue()));
}
}

// 设置参数
if (data != null && data.size() > 0) {
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
for (Map.Entry<String, Object> entry : data.entrySet()) {
Object value = entry.getValue();
if (value instanceof List) {
for (Object v : (List) value) {
builder.addPart(entry.getKey(), new StringBody(v.toString(), ContentType.TEXT_PLAIN));
}
} else {
builder.addPart(entry.getKey(), new StringBody(value.toString(), ContentType.TEXT_PLAIN));
}
}

HttpEntity httpEntity = builder.build();
httpPost.setEntity(httpEntity);
}

HttpResponse response = client.execute(httpPost);
if (response != null) {
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
result = EntityUtils.toString(resEntity);
}
}

return result;
}
}