|
@ -0,0 +1,295 @@
|
|
1
|
package com.ai.bss.security.protection.utils;
|
|
2
|
|
|
3
|
import com.alibaba.fastjson.JSONObject;
|
|
4
|
import org.apache.http.HttpEntity;
|
|
5
|
import org.apache.http.HttpStatus;
|
|
6
|
import org.apache.http.client.ClientProtocolException;
|
|
7
|
import org.apache.http.client.HttpClient;
|
|
8
|
import org.apache.http.client.config.RequestConfig;
|
|
9
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
10
|
import org.apache.http.client.methods.HttpGet;
|
|
11
|
import org.apache.http.client.methods.HttpPost;
|
|
12
|
import org.apache.http.client.methods.HttpPut;
|
|
13
|
import org.apache.http.entity.StringEntity;
|
|
14
|
import org.apache.http.entity.mime.MultipartEntityBuilder;
|
|
15
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
16
|
import org.apache.http.impl.client.HttpClients;
|
|
17
|
import org.apache.http.protocol.HTTP;
|
|
18
|
import org.apache.http.util.EntityUtils;
|
|
19
|
import org.slf4j.Logger;
|
|
20
|
import org.slf4j.LoggerFactory;
|
|
21
|
import org.springframework.util.CollectionUtils;
|
|
22
|
|
|
23
|
import java.io.*;
|
|
24
|
import java.net.HttpURLConnection;
|
|
25
|
import java.net.URL;
|
|
26
|
import java.net.URLEncoder;
|
|
27
|
import java.nio.charset.Charset;
|
|
28
|
import java.util.Iterator;
|
|
29
|
import java.util.Map;
|
|
30
|
import java.util.Set;
|
|
31
|
|
|
32
|
/**
|
|
33
|
* http服务请求工具类
|
|
34
|
*
|
|
35
|
* @author chencai
|
|
36
|
*/
|
|
37
|
public class HttpServiceUtil {
|
|
38
|
static Logger log = LoggerFactory.getLogger(HttpServiceUtil.class);
|
|
39
|
|
|
40
|
private static final String HTTP_CONTENT_TYPE_JSON = "application/json; charset=utf-8";
|
|
41
|
|
|
42
|
public static String buildUrl(Map<String, String> paramMap, String serviceUrl) {
|
|
43
|
String url = null;
|
|
44
|
StringBuffer urlString = new StringBuffer();
|
|
45
|
urlString.append(serviceUrl);
|
|
46
|
|
|
47
|
if (!paramMap.isEmpty()) {
|
|
48
|
// 参数列表不为空,地址尾部增加'?'
|
|
49
|
urlString.append('?');
|
|
50
|
// 拼接参数
|
|
51
|
Set<Map.Entry<String, String>> entrySet = paramMap.entrySet();
|
|
52
|
for (Map.Entry<String, String> entry : entrySet) {
|
|
53
|
try {
|
|
54
|
urlString.append(entry.getKey()).append('=').append(URLEncoder.encode(entry.getValue(), "UTF-8"))
|
|
55
|
.append('&');
|
|
56
|
} catch (UnsupportedEncodingException e) {
|
|
57
|
log.error(" Exception: " + e);
|
|
58
|
}
|
|
59
|
}
|
|
60
|
// 去掉最后一个字符“&”
|
|
61
|
url = urlString.substring(0, urlString.length() - 1);
|
|
62
|
}
|
|
63
|
return url;
|
|
64
|
}
|
|
65
|
|
|
66
|
public static String sendRequest(String url) {
|
|
67
|
InputStream inputStream = null;
|
|
68
|
BufferedReader bufferedReader = null;
|
|
69
|
HttpURLConnection httpURLConnection = null;
|
|
70
|
try {
|
|
71
|
log.error("It's not error. request url: " + url);
|
|
72
|
URL requestURL = new URL(url);
|
|
73
|
// 获取连接
|
|
74
|
httpURLConnection = (HttpURLConnection) requestURL.openConnection();
|
|
75
|
httpURLConnection.setConnectTimeout(10000); // 建立连接的超时时间,毫秒
|
|
76
|
httpURLConnection.setReadTimeout(25000); // 获得返回的超时时间,毫秒
|
|
77
|
httpURLConnection.setRequestMethod("GET");
|
|
78
|
httpURLConnection.setRequestProperty("Content-type", "text/html;charset=UTF-8");
|
|
79
|
httpURLConnection.setRequestProperty("Accept",
|
|
80
|
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
|
|
81
|
// 通过输入流获取请求的内容
|
|
82
|
inputStream = httpURLConnection.getInputStream();
|
|
83
|
bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
|
|
84
|
String temp = null;
|
|
85
|
StringBuffer stringBuffer = new StringBuffer();
|
|
86
|
// 循环读取返回的结果
|
|
87
|
while ((temp = bufferedReader.readLine()) != null) {
|
|
88
|
stringBuffer.append(temp);
|
|
89
|
}
|
|
90
|
|
|
91
|
return stringBuffer.toString();
|
|
92
|
} catch (Exception e) {
|
|
93
|
log.error("sendRequest Exception: " + e);
|
|
94
|
} finally {
|
|
95
|
// 断开连接
|
|
96
|
if (httpURLConnection != null) {
|
|
97
|
httpURLConnection.disconnect();
|
|
98
|
}
|
|
99
|
// 关闭流
|
|
100
|
if (bufferedReader != null) {
|
|
101
|
try {
|
|
102
|
bufferedReader.close();
|
|
103
|
} catch (IOException e) {
|
|
104
|
log.error("bufferedReader Exception: " + e);
|
|
105
|
}
|
|
106
|
}
|
|
107
|
if (inputStream != null) {
|
|
108
|
try {
|
|
109
|
inputStream.close();
|
|
110
|
} catch (IOException e) {
|
|
111
|
log.error("inputStream Exception: " + e);
|
|
112
|
}
|
|
113
|
}
|
|
114
|
}
|
|
115
|
return null;
|
|
116
|
}
|
|
117
|
|
|
118
|
public static String sendPostRequest(String url, Map<String, String> map, String encoding) {
|
|
119
|
String retStr = "";
|
|
120
|
try {
|
|
121
|
retStr = sendPostRequest(url, map, encoding, 0, 0);
|
|
122
|
} catch (Exception ex) {
|
|
123
|
log.error("sendPostRequest error: " + ex);
|
|
124
|
}
|
|
125
|
|
|
126
|
return retStr;
|
|
127
|
}
|
|
128
|
|
|
129
|
public static String sendPostRequest(String url, Map<String, String> map, String encoding, int ebossConnectTimeout,
|
|
130
|
int ebossServiceTimeout) {
|
|
131
|
CloseableHttpClient httpClient = HttpClients.createDefault();
|
|
132
|
HttpPost httpPost = new HttpPost(url);
|
|
133
|
String body = "";
|
|
134
|
|
|
135
|
try {
|
|
136
|
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
|
|
137
|
if (map != null) {
|
|
138
|
for (Map.Entry<String, String> entry : map.entrySet()) {
|
|
139
|
multipartEntityBuilder.addTextBody(entry.getKey(), entry.getValue());
|
|
140
|
}
|
|
141
|
}
|
|
142
|
|
|
143
|
log.debug("ebossConnectTimeout:" + ebossConnectTimeout);
|
|
144
|
log.debug("ebossServiceTimeout:" + ebossServiceTimeout);
|
|
145
|
|
|
146
|
httpPost.setEntity(multipartEntityBuilder.build());
|
|
147
|
if (ebossConnectTimeout != 0 && ebossServiceTimeout != 0) {
|
|
148
|
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(ebossServiceTimeout)
|
|
149
|
.setConnectTimeout(ebossConnectTimeout).build();// 设置请求和传输超时时间
|
|
150
|
httpPost.setConfig(requestConfig);
|
|
151
|
}
|
|
152
|
|
|
153
|
CloseableHttpResponse response = httpClient.execute(httpPost);
|
|
154
|
|
|
155
|
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
|
|
156
|
HttpEntity entity = response.getEntity();
|
|
157
|
if (entity != null) {
|
|
158
|
// 按指定编码转换结果实体为String类型
|
|
159
|
body = EntityUtils.toString(entity, encoding);
|
|
160
|
}
|
|
161
|
EntityUtils.consume(entity);
|
|
162
|
}
|
|
163
|
} catch (ClientProtocolException e) {
|
|
164
|
log.error("ClientProtocolException Exception: " + e);
|
|
165
|
} catch (UnsupportedEncodingException e) {
|
|
166
|
log.error("UnsupportedEncodingException Exception: " + e);
|
|
167
|
} catch (IOException e) {
|
|
168
|
log.error("IOException Exception: " + e);
|
|
169
|
// throw new BaseException("10", "调用Eboss超时");
|
|
170
|
} finally {
|
|
171
|
// 关闭连接,释放资源
|
|
172
|
try {
|
|
173
|
httpClient.close();
|
|
174
|
} catch (IOException e) {
|
|
175
|
log.error("httpClient Exception: " + e);
|
|
176
|
}
|
|
177
|
}
|
|
178
|
|
|
179
|
return body;
|
|
180
|
}
|
|
181
|
|
|
182
|
public static String sendGet(String url, Charset encoding, Map<String, String> headerMap) {
|
|
183
|
HttpClient httpClient = HttpClients.createDefault();
|
|
184
|
HttpGet httpGet = new HttpGet(url);
|
|
185
|
httpGet.addHeader(HTTP.CONTENT_TYPE, HTTP_CONTENT_TYPE_JSON);
|
|
186
|
CloseableHttpResponse response = null;
|
|
187
|
try {
|
|
188
|
log.debug("执行GET请求:url= " + url);
|
|
189
|
response = (CloseableHttpResponse) httpClient.execute(httpGet);
|
|
190
|
} catch (Exception e) {
|
|
191
|
log.error("sendGet Exception: " + e.getMessage());
|
|
192
|
return "{}";
|
|
193
|
}
|
|
194
|
return responseEx(httpClient, response, encoding);
|
|
195
|
}
|
|
196
|
|
|
197
|
public static String sendPost(String url, Map<String, Object> paramsMap, Charset encoding) {
|
|
198
|
HttpClient httpClient = HttpClients.createDefault();
|
|
199
|
HttpPost httpPost = new HttpPost(url);
|
|
200
|
httpPost.addHeader(HTTP.CONTENT_TYPE, HTTP_CONTENT_TYPE_JSON);
|
|
201
|
if (!CollectionUtils.isEmpty(paramsMap)) {
|
|
202
|
StringEntity se = new StringEntity(JSONObject.toJSONString(paramsMap), encoding);
|
|
203
|
httpPost.setEntity(se);
|
|
204
|
}
|
|
205
|
CloseableHttpResponse response = null;
|
|
206
|
try {
|
|
207
|
log.debug("执行POST无参请求:url= " + url);
|
|
208
|
response = (CloseableHttpResponse) httpClient.execute(httpPost);
|
|
209
|
} catch (Exception e) {
|
|
210
|
log.error("sendPost Exception: " + e.getMessage());
|
|
211
|
return "{}";
|
|
212
|
}
|
|
213
|
return responseEx(httpClient, response, encoding);
|
|
214
|
}
|
|
215
|
|
|
216
|
public static String sendPost(String url, Map<String, Object> paramsMap, Charset encoding,
|
|
217
|
Map<String, String> headerMap) {
|
|
218
|
HttpClient httpClient = HttpClients.createDefault();
|
|
219
|
HttpPost httpPost = new HttpPost(url);
|
|
220
|
|
|
221
|
Iterator<String> iter = headerMap.keySet().iterator();
|
|
222
|
String key = "";
|
|
223
|
String value = "";
|
|
224
|
while (iter.hasNext()) {
|
|
225
|
key = iter.next();
|
|
226
|
value = headerMap.get(key);
|
|
227
|
httpPost.setHeader(key, value);
|
|
228
|
}
|
|
229
|
|
|
230
|
httpPost.addHeader(HTTP.CONTENT_TYPE, HTTP_CONTENT_TYPE_JSON);
|
|
231
|
if (!CollectionUtils.isEmpty(paramsMap)) {
|
|
232
|
log.debug("执行POST请求:url= " + url + ",参数= " + JSONObject.toJSONString(paramsMap));
|
|
233
|
StringEntity se = new StringEntity(JSONObject.toJSONString(paramsMap), encoding);
|
|
234
|
httpPost.setEntity(se);
|
|
235
|
}
|
|
236
|
CloseableHttpResponse response = null;
|
|
237
|
try {
|
|
238
|
response = (CloseableHttpResponse) httpClient.execute(httpPost);
|
|
239
|
} catch (Exception e) {
|
|
240
|
log.error("sendPost Exception: " + e.getMessage());
|
|
241
|
return "{}";
|
|
242
|
}
|
|
243
|
return responseEx(httpClient, response, encoding);
|
|
244
|
}
|
|
245
|
|
|
246
|
public static String sendPut(String url, Map<String, Object> paramsMap, Charset encoding) {
|
|
247
|
HttpClient httpClient = HttpClients.createDefault();
|
|
248
|
String resp = "";
|
|
249
|
HttpPut httpPut = new HttpPut(url);
|
|
250
|
httpPut.addHeader(HTTP.CONTENT_TYPE, HTTP_CONTENT_TYPE_JSON);
|
|
251
|
if (!CollectionUtils.isEmpty(paramsMap)) {
|
|
252
|
StringEntity se = new StringEntity(JSONObject.toJSONString(paramsMap), encoding);
|
|
253
|
httpPut.setEntity(se);
|
|
254
|
}
|
|
255
|
CloseableHttpResponse response = null;
|
|
256
|
try {
|
|
257
|
log.debug("执行PUT请求:url= " + url + ",参数= " + JSONObject.toJSONString(paramsMap));
|
|
258
|
response = (CloseableHttpResponse) httpClient.execute(httpPut);
|
|
259
|
} catch (Exception e) {
|
|
260
|
log.error("sendPut Exception: " + e.getMessage());
|
|
261
|
return "{}";
|
|
262
|
}
|
|
263
|
|
|
264
|
return responseEx(httpClient, response, encoding);
|
|
265
|
}
|
|
266
|
|
|
267
|
private static String responseEx(HttpClient httpClient, CloseableHttpResponse response, Charset encoding) {
|
|
268
|
String resp = "";
|
|
269
|
try {
|
|
270
|
if (response == null) {
|
|
271
|
resp = "{}";
|
|
272
|
log.error("请求执行失败,无返回信息");
|
|
273
|
} else if (200 != response.getStatusLine().getStatusCode()) {
|
|
274
|
resp = "{}";
|
|
275
|
log.error("请求执行失败,状态码=" + response.getStatusLine().getStatusCode() + ",返回信息="
|
|
276
|
+ EntityUtils.toString(response.getEntity(), encoding));
|
|
277
|
} else {
|
|
278
|
log.debug("请求执行成功");
|
|
279
|
resp = EntityUtils.toString(response.getEntity(), encoding);
|
|
280
|
}
|
|
281
|
} catch (Exception e) {
|
|
282
|
log.error("responseEx Exception: " + e.getMessage());
|
|
283
|
} finally {
|
|
284
|
if (response != null) {
|
|
285
|
try {
|
|
286
|
response.close();
|
|
287
|
} catch (Exception e) {
|
|
288
|
log.error("responseEx Exception: " + e.getMessage());
|
|
289
|
}
|
|
290
|
}
|
|
291
|
}
|
|
292
|
return resp;
|
|
293
|
}
|
|
294
|
|
|
295
|
}
|