HttpUtil.java
17.5 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
package com.subsidy.util;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
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.client.methods.HttpUriRequest;
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.FileBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.net.URLEncoder;
import java.util.*;
/**
* @author: sadboy
**/
public class HttpUtil {
private static final Logger log = LoggerFactory.getLogger(HttpUtil.class);
private static final String UTF8 = "UTF-8";
private static final String APPLICATION_FORM_URLENCODED = "application/x-www-form-urlencoded; charset=UTF-8";
private static final String APPLICATION_JSON = "application/json; charset=UTF-8";
private static final String TEXT_PLAIN = "text/plain; charset=UTF-8";
private static final String TEXT_HTML = "text/html; charset=UTF-8";
private static final String APPLICATION_XML = "application/xml; charset=UTF-8";
private HttpUtil() {
}
/**
* 向url发送get请求,当无参数时,paramMap为NULL
* @param url 请求url
* @param paramMap 需要拼接的参数
* @return 请求返回的数据
* @throws IOException 读写异常
*/
public static String get(String url, Map<String, String> paramMap) throws IOException {
return get(url, paramMap, UTF8);
}
/**
* 向url发送get请求
* @param url 请求url
* @param paramMap 需要拼接的参数
* @param encoding 编码
* @return 请求返回的数据
* @throws IOException 读写异常
*/
public static String get(String url, Map<String, String> paramMap, String encoding) throws IOException {
encoding = encoding == null ? UTF8 : encoding;
url = appendUrl(url, paramMap);
return get(url, encoding, new DataParse<String>() {
@Override
public String parseData(HttpEntity httpEntity, String encoding) throws IOException {
return EntityUtils.toString(httpEntity, encoding);
}
});
}
/**
* 向url发送get请求
* @param url 请求url
* @param paramMap 需要拼接的参数
* @param encoding 编码
* @return 请求返回的字节数组,一般用于文件下载
* @throws IOException 读写异常
*/
public static byte[] getBinary(String url, Map<String, String> paramMap, String encoding) throws IOException {
encoding = encoding == null ? UTF8 : encoding;
url = appendUrl(url, paramMap);
return get(url, encoding, new DataParse<byte[]>() {
@Override
public byte[] parseData(HttpEntity httpEntity, String encoding) throws IOException {
return EntityUtils.toByteArray(httpEntity);
}
});
}
/**
* HTTP GET 内部公共请求处理逻辑
* @param url 请求地址
* @param encoding 编码字符集, 默认为 utf-8
* @param dataParse 返回数据反序列化逻辑实现类
* @return HTTP 返回的内容
* @throws IOException 客户端和服务器读写通讯异常
*/
private static <T> T get(String url, String encoding, DataParse<T> dataParse) throws IOException {
log.debug("http 请求 url: {}", url);
T result = null;
// 创建httpclient对象
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
// 创建get方式请求对象
HttpGet httpGet = new HttpGet(url);
httpGet.addHeader("Content-type", APPLICATION_JSON);
// 通过请求对象获取响应对象
CloseableHttpResponse response = sendRequestAndGetResult(url, httpClient, httpGet);
// 获取结果实体
if (null != response) {
result = dataParse.parseData(response.getEntity(), encoding);
if (!(result instanceof byte[])) {
log.debug("http 请求结果: {}", result);
} else {
Header[] headers = response.getHeaders("Content-Type");
for (Header responseHead : headers) {
String headStr = responseHead.getValue();
if (headStr.startsWith("application/json")) {
String json = new String((byte[]) result);
response.close();
throw new RuntimeException(json);
}
}
}
}
try {
if (null != response) {
response.close();
}
} catch (IOException ex) {
log.error(ex.getMessage(), ex);
}
return result;
}
/**
* 向url发送post请求
* @param url 请求url
* @param paramMap 需要拼接的参数
* @return 请求返回的数据
* @throws IOException 读写异常
*/
public static String postFormBody(String url, Map<String, String> paramMap) throws IOException {
return postFormBody(url, paramMap, null);
}
/**
* 向url发送post请求
* @param url 请求url
* @param paramMap 需要拼接的参数
* @param encoding 编码
* @return 请求返回的数据
* @throws IOException 读写异常
*/
public static String postFormBody(String url, Map<String, String> paramMap, String encoding) throws IOException {
return post(url, paramMap, encoding);
}
/**
* 向url发送post请求表单提交数据
* @param url 请求url
* @param paramMap 表单数据
* @param encoding 编码
* @return 请求返回的数据
* @throws IOException 读写异常
*/
private static String post(String url, Map<String, String> paramMap, String encoding) throws IOException {
log.debug("http 请求 url: {} , 请求参数: {}", url, appendUrl("", paramMap).replace("?", ""));
encoding = encoding == null ? UTF8 : encoding;
// 创建post方式请求对象
HttpPost httpPost = new HttpPost(url);
// 装填参数
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
if (paramMap != null) {
for (Map.Entry<String, String> entry : paramMap.entrySet()) {
String value = entry.getValue();
//去掉如下判断会造成String类型的value为null时
if (value != null) {
nameValuePairs.add(new BasicNameValuePair(entry.getKey(), value));
}
}
}
// 设置参数到请求对象中
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, encoding));
// 设置header信息
// 指定报文头【Content-type】、【User-Agent】
httpPost.setHeader("Content-type", APPLICATION_FORM_URLENCODED);
return post(url, httpPost, encoding, new DataParse<String>() {
@Override
public String parseData(HttpEntity httpEntity, String encoding) throws IOException {
return EntityUtils.toString(httpEntity, encoding);
}
});
}
/**
* 向url发送post请求发送json
* @param url 请求url
* @param json json字符串
* @param encoding 编码
* @return 请求返回的数据
* @throws IOException 读写异常
*/
public static String postJsonBody(String url, String json, String encoding) throws IOException {
log.debug("http 请求 url: {} , 请求参数: {}", url, json);
encoding = encoding == null ? UTF8 : encoding;
// 创建post方式请求对象
HttpPost httpPost = new HttpPost(url);
// 设置参数到请求对象中
StringEntity stringEntity = new StringEntity(json, ContentType.APPLICATION_JSON);
// Constant.UTF8
stringEntity.setContentEncoding(encoding);
httpPost.setEntity(stringEntity);
String result = post(url, httpPost, encoding, new DataParse<String>() {
@Override
public String parseData(HttpEntity httpEntity, String encoding) throws IOException {
return EntityUtils.toString(httpEntity, encoding);
}
});
return result;
}
/**
* 向url发送post请求
* @param url 请求url
* @param httpPost httpClient
* @return 请求返回的数据
* @throws IOException 读写异常
*/
private static <T> T post(String url, HttpPost httpPost, String encoding, DataParse<T> dataParse)
throws IOException {
T result = null;
CloseableHttpResponse response = null;
// 创建httpclient对象
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
// 执行请求操作,并拿到结果(同步阻塞)
response = sendRequestAndGetResult(url, httpClient, httpPost);
// 获取结果实体
// 判断网络连接状态码是否正常(0--200都数正常)
if (null != response) {
result = dataParse.parseData(response.getEntity(), encoding);
log.debug("http 请求结果: {}", result);
}
try {
if (null != response) {
response.close();
}
} catch (IOException ex) {
log.error(ex.getMessage(), ex);
}
return result;
}
/**
* 设置http头,发送http请求,打印请求耗时
* @param url 请求url
* @param httpClient httpClient
* @param httpUriRequest httpUriRequest
* @return 请求返回的数据
* @throws IOException 读写异常
*/
private static CloseableHttpResponse sendRequestAndGetResult(String url, CloseableHttpClient httpClient,
HttpUriRequest httpUriRequest) throws IOException {
long startTime = System.currentTimeMillis();
CloseableHttpResponse response = httpClient.execute(httpUriRequest);
long endTime = System.currentTimeMillis();
collectAPISpendTime(url, startTime, endTime);
return response;
}
/**
* 打印请求信息
* @param url 请求url
* @param startTime 请求开始时间
* @param endTime 请求结束时间
*/
private static void collectAPISpendTime(String url, long startTime, long endTime) {
log.debug("HTTP请求耗时分析,请求URL: {} , 耗时: {} ms", url, endTime - startTime);
}
// /**
// * 向url发送post请求上传单文件
// * @param url 请求url
// * @param paramMap 需要表单提交的参数
// * @param fileMap 需要上传的文件
// * @param encoding 编码
// * @return 请求返回的数据
// * @throws IOException 读写异常
// */
// public static String postFile(String url, Map<String, String> paramMap, Map<String, File> fileMap, String encoding)
// throws IOException {
// if (fileMap != null) {
// Map<String, List<File>> fileListMap = new HashMap<String, List<File>>();
// for (Map.Entry<String, File> entry : fileMap.entrySet()) {
// File file = entry.getValue();
// List<File> fileList = new ArrayList<File>();
// fileList.add(file);
// fileListMap.put(entry.getKey(), fileList);
// }
// return postMultipleFile(url, paramMap, fileListMap, encoding);
// }
// return postMultipleFile(url, paramMap, null, encoding);
// }
// /**
// * 向url发送post请求上传多文件
// * 向url发送post请求上传单文件
// * @param url 请求url
// * @param paramMap 需要表单提交的参数
// * @param fileListMap 需要上传的文件
// * @param encoding 编码
// * @return 请求返回的数据
// * @throws IOException 读写异常
// */
// public static String postMultipleFile(String url, Map<String, String> paramMap, Map<String, List<File>> fileListMap,
// String encoding) throws IOException {
// return postFileBody(url, paramMap, fileListMap, encoding, new DataParse<String>() {
// @Override
// public String parseData(HttpEntity httpEntity, String encoding) throws IOException {
// return EntityUtils.toString(httpEntity, encoding);
// }
// });
// }
// /**
// * 向url发送post请求上传多文件
// * 向url发送post请求上传单文件
// * @param url 请求url
// * @param paramMap 需要表单提交的参数
// * @param fileListMap 需要上传的文件
// * @param encoding 编码
// * @return 请求返回的数据
// * @throws IOException 读写异常
// */
// private static <T> T postFileBody(String url, Map<String, String> paramMap, Map<String, List<File>> fileListMap,
// String encoding, DataParse<T> dataParse) throws IOException {
// log.debug("http 请求 url: {} , 请求参数: {}", url, appendUrl("", paramMap).replace("?", ""));
// encoding = encoding == null ? UTF8 : encoding;
// T result = null;
// CloseableHttpClient httpClient = HttpClientBuilder.create().build();
// HttpPost httpPost = new HttpPost(url);
// MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
//
// ContentType contentType = ContentType.create("text/plain", Charset.forName(encoding));
// if (null != paramMap) {
// for (Map.Entry<String, String> entry : paramMap.entrySet()) {
// entityBuilder.addTextBody(entry.getKey(), entry.getValue(), contentType);
// }
// }
//
// if (null != fileListMap) {
// for (Map.Entry<String, List<File>> entry : fileListMap.entrySet()) {
// String key = entry.getKey();
// List<File> fileList = entry.getValue();
// for (File file : fileList) {
// FileBody fileBody = new FileBody(file);
// entityBuilder.addPart(key, fileBody);
// }
// }
// }
//
// HttpEntity entity = entityBuilder.build();
// httpPost.setEntity(entity);
// CloseableHttpResponse response = sendRequestAndGetResult(url, httpClient, httpPost);
// if (null != response) {
// result = dataParse.parseData(response.getEntity(), encoding);
// log.debug("http 请求结果: {}", result);
// }
// try {
// if (null != response) {
// response.close();
// }
// } catch (IOException ex) {
// log.error(ex.getMessage(), ex);
// }
// return result;
// }
/**
* 公共数据解析接口
* @param <T>
*/
private interface DataParse<T> {
/**
* 解析返回数据
* @param httpEntity 返回实体
* @param encoding 编码
* @return 实际解析返回内容
* @throws IOException io异常
*/
T parseData(HttpEntity httpEntity, String encoding) throws IOException;
}
/**
* 将url与map拼接成HTTP查询字符串
* @param url 请求url
* @param paramMap 需要拼装的map
* @return 拼装好的url
*/
public static String appendUrl(String url, Map<String, String> paramMap) throws UnsupportedEncodingException {
if (paramMap == null) {
return url;
}
StringBuffer paramStringBuffer = new StringBuffer();
Iterator<Map.Entry<String, String>> mapIterator = paramMap.entrySet().iterator();
while (mapIterator.hasNext()) {
Map.Entry<String, String> next = mapIterator.next();
paramStringBuffer.append(next.getKey()).append("=").append(URLEncoder.encode(next.getValue(), ConstantUtils.UTF8)).append("&");
}
String paramStr = paramStringBuffer.toString();
// String paramStr = mapJoinNotEncode(paramMap);
if (paramStr != null && !"".equals(paramStr)) {
if (url.indexOf("?") > 0) {
if (url.endsWith("&")) {
url += paramStr.substring(0, paramStr.length() - 1);
} else {
url += "&" + paramStr.substring(0, paramStr.length() - 1);
}
} else {
url += "?" + paramStr.substring(0, paramStr.length() - 1);
}
}
return url;
}
/**
* 把二进制写入文件
* @param bytes
* @param path
* @throws IOException
*/
public static void writeFile(byte[] bytes, String path) throws IOException {
OutputStream os = null;
try {
// 根据绝对路径初始化文件
File localFile = new File(path);
if (!localFile.exists()) {
boolean newFile = localFile.createNewFile();
if (!newFile) {
throw new RuntimeException("创建文件异常,路径:" + path);
}
}
// 输出流
os = new FileOutputStream(localFile);
os.write(bytes);
} finally {
if (os != null) {
os.close();
}
}
}
}