HttpUtil.java
16.9 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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
package com.cnlive.shenhe.utils;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.config.RequestConfig.Builder;
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.client.utils.URIBuilder;
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.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.UnsupportedCharsetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
/**
* http请求工具类
*
* @author liujiong
*/
public class HttpUtil {
private Logger logger = LoggerFactory.getLogger(HttpUtil.class);
/**
* httpclient连接池
*/
private static PoolingHttpClientConnectionManager pcm;
/**
* http连接
*/
private CloseableHttpClient httpClient = null;
/**
* 连接超时时间
*/
private int connectTimeout = 120000;
/**
* 从连接池获取连接超时时间
*/
private int connectionRequestTimeout = 10000;
/**
* 获取数据超时时间
*/
private int socketTimeout = 300000;
private String charset = "utf-8";
/**
* 请求配置
*/
private RequestConfig requestConfig = null;
/**
* build requestConfig
*/
private Builder requestConfigBuilder = null;
private List<NameValuePair> nvps = new ArrayList<>();
private List<Header> headers = new ArrayList<>();
private String requestParam = "";
static {
pcm = new PoolingHttpClientConnectionManager();
pcm.setMaxTotal(50);//整个连接池最大连接数
pcm.setDefaultMaxPerRoute(50);//每路由最大连接数,默认值是2
}
/**
* 默认设置
*
* @author Liu Jiong
* @createDate 2016年10月30日
*/
private static HttpUtil defaultInit() {
HttpUtil httpUtil = new HttpUtil();
if (httpUtil.requestConfig == null) {
httpUtil.requestConfigBuilder = RequestConfig.custom().setConnectTimeout(httpUtil.connectTimeout)
.setConnectionRequestTimeout(httpUtil.connectionRequestTimeout)
.setSocketTimeout(httpUtil.socketTimeout);
httpUtil.requestConfig = httpUtil.requestConfigBuilder.build();
}
return httpUtil;
}
/**
* 初始化 httpUtil
*/
public static HttpUtil init() {
HttpUtil httpUtil = defaultInit();
if (httpUtil.httpClient == null) {
httpUtil.httpClient = HttpClients.custom().setConnectionManager(pcm).build();
}
return httpUtil;
}
/**
* 初始化 httpUtil
*/
public static HttpUtil init(Map<String, String> paramMap) {
HttpUtil httpUtil = init();
httpUtil.setParamMap(paramMap);
return httpUtil;
}
/**
* 验证初始化
*/
public static HttpUtil initWithAuth(String ip, int port, String username, String password) {
HttpUtil httpUtil = defaultInit();
if (httpUtil.httpClient == null) {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(new AuthScope(ip, port, AuthScope.ANY_REALM), new UsernamePasswordCredentials(username, password));
httpUtil.httpClient = HttpClients.custom().setDefaultCredentialsProvider(credentialsProvider)
.setConnectionManager(pcm).build();
}
return httpUtil;
}
/**
* 设置请求头
*/
public HttpUtil setHeader(String name, String value) {
Header header = new BasicHeader(name, value);
headers.add(header);
return this;
}
/**
* 设置请求头
*/
public HttpUtil setHeaderMap(Map<String, String> headerMap) {
for (Entry<String, String> param : headerMap.entrySet()) {
Header header = new BasicHeader(param.getKey(), param.getValue());
headers.add(header);
}
return this;
}
/**
* 设置请求参数
*/
public HttpUtil setParam(String name, String value) {
nvps.add(new BasicNameValuePair(name, value));
return this;
}
/**
* 设置请求参数
*/
public HttpUtil setParamMap(Map<String, String> paramMap) {
for (Entry<String, String> param : paramMap.entrySet()) {
nvps.add(new BasicNameValuePair(param.getKey(), param.getValue()));
}
return this;
}
/**
* 设置字符串参数
*/
public HttpUtil setStringParam(String requestParam) {
this.requestParam = requestParam;
return this;
}
/**
* 设置连接超时时间
*/
public HttpUtil setConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
this.requestConfigBuilder = requestConfigBuilder.setConnectTimeout(connectTimeout);
requestConfig = requestConfigBuilder.build();
return this;
}
/**
* http get 请求
*/
public Map<String, String> get(String url) {
Map<String, String> resultMap = new HashMap<>();
//获取请求URI
URI uri = getUri(url);
if (uri != null) {
HttpGet httpGet = new HttpGet(uri);
httpGet.setConfig(requestConfig);
if (!CollectionUtils.isEmpty(headers)) {
Header[] header = new Header[headers.size()];
httpGet.setHeaders(headers.toArray(header));
}
//执行get请求
try {
CloseableHttpResponse response = httpClient.execute(httpGet);
return getHttpResult(response, url, httpGet, resultMap);
} catch (Exception e) {
httpGet.abort();
resultMap.put("result", e.getMessage());
logger.error("获取http GET请求返回值失败 url======" + url, e);
}
}
return resultMap;
}
/**
* http post 请求
*/
public Map<String, String> post(String url) {
HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(requestConfig);
if (!CollectionUtils.isEmpty(headers)) {
Header[] header = new Header[headers.size()];
httpPost.setHeaders(headers.toArray(header));
}
if (!CollectionUtils.isEmpty(nvps)) {
try {
httpPost.setEntity(new UrlEncodedFormEntity(nvps, charset));
} catch (UnsupportedEncodingException e) {
logger.error("http post entity form error", e);
}
}
if (!StringUtils.isEmpty(requestParam)) {
try {
httpPost.setEntity(new StringEntity(requestParam, charset));
} catch (UnsupportedCharsetException e) {
logger.error("http post entity form error", e);
}
}
Map<String, String> resultMap = new HashMap<>();
//执行post请求
try {
CloseableHttpResponse response = httpClient.execute(httpPost);
return getHttpResult(response, url, httpPost, resultMap);
} catch (Exception e) {
httpPost.abort();
resultMap.put("result", e.getMessage());
logger.error("获取http POST请求返回值失败 url======" + url, e);
}
return resultMap;
}
/**
* post 上传文件
*/
public Map<String, String> postUploadFile(String url, Map<String, File> fileParam) {
HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(requestConfig);
if (!CollectionUtils.isEmpty(headers)) {
Header[] header = new Header[headers.size()];
httpPost.setHeaders(headers.toArray(header));
}
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
if (fileParam != null) {
for (Entry<String, File> entry : fileParam.entrySet()) {
//将要上传的文件转化为文件流
FileBody fileBody = new FileBody(entry.getValue());
//设置请求参数
builder.addPart(entry.getKey(), fileBody);
}
}
if (!CollectionUtils.isEmpty(nvps)) {
for (NameValuePair nvp : nvps) {
String value = nvp.getValue();
if (!StringUtils.isEmpty(value)) {
builder.addTextBody(nvp.getName(), value, ContentType.create("text/plain", charset));
}
}
}
httpPost.setEntity(builder.build());
Map<String, String> resultMap = new HashMap<>();
//执行post请求
try {
CloseableHttpResponse response = httpClient.execute(httpPost);
return getHttpResult(response, url, httpPost, resultMap);
} catch (Exception e) {
httpPost.abort();
resultMap.put("result", e.getMessage());
logger.error("获取http postUploadFile 请求返回值失败 url======" + url, e);
}
return resultMap;
}
/**
* 获取请求返回值
*/
private Map<String, String> getHttpResult(CloseableHttpResponse response, String url, HttpUriRequest request, Map<String, String> resultMap) {
String result = "";
int statusCode = response.getStatusLine().getStatusCode();
resultMap.put("statusCode", statusCode + "");
HttpEntity entity = response.getEntity();
if (entity != null) {
try {
result = EntityUtils.toString(entity, charset);
//释放连接
EntityUtils.consume(entity);
} catch (Exception e) {
logger.error("获取http请求返回值解析失败", e);
request.abort();
}
}
if (statusCode != 200) {
result = "HttpClient status code :" + statusCode + " request url===" + url;
logger.info("HttpClient status code :" + statusCode + " request url===" + url);
request.abort();
}
resultMap.put("result", result);
return resultMap;
}
/**
* 获取重定向url返回的location
*/
public String redirectLocation(String url) {
String location = "";
//获取请求URI
URI uri = getUri(url);
if (uri != null) {
HttpGet httpGet = new HttpGet(uri);
requestConfig = requestConfigBuilder.setRedirectsEnabled(false).build();//设置自动重定向false
httpGet.setConfig(requestConfig);
if (!CollectionUtils.isEmpty(headers)) {
Header[] header = new Header[headers.size()];
httpGet.setHeaders(headers.toArray(header));
}
try {
//执行get请求
CloseableHttpResponse response = httpClient.execute(httpGet);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {//301 302
Header header = response.getFirstHeader("Location");
if (header != null) {
location = header.getValue();
}
}
HttpEntity entity = response.getEntity();
if (entity != null) {
EntityUtils.consume(entity);
}
} catch (Exception e) {
logger.error("获取http GET请求获取 302 Location失败 url======" + url, e);
httpGet.abort();
}
}
return location;
}
/**
* 获取输入流
*/
public InputStream getInputStream(String url) {
//获取请求URI
URI uri = getUri(url);
if (uri != null) {
HttpGet httpGet = new HttpGet(uri);
httpGet.setConfig(requestConfig);
if (!CollectionUtils.isEmpty(headers)) {
Header[] header = new Header[headers.size()];
httpGet.setHeaders(headers.toArray(header));
}
//执行get请求
try {
CloseableHttpResponse response = httpClient.execute(httpGet);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != 200) {
logger.info("HttpClient status code :" + statusCode + " request url===" + url);
httpGet.abort();
} else {
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream in = entity.getContent();
return in;
}
}
} catch (Exception e) {
logger.error("获取http GET inputStream请求失败 url======" + url, e);
httpGet.abort();
}
}
return null;
}
private URI getUri(String url) {
URI uri = null;
try {
URIBuilder uriBuilder = new URIBuilder(url);
if (!CollectionUtils.isEmpty(nvps)) {
uriBuilder.setParameters(nvps);
}
uri = uriBuilder.build();
} catch (URISyntaxException e) {
logger.error("url 地址异常", e);
}
return uri;
}
/**
* from请求
*
* @param url
* @param params
* @return
*/
public static String form(String url, Map<String, String> params) {
URL u = null;
HttpURLConnection con = null;
// 构建请求参数
StringBuffer sb = new StringBuffer();
if (params != null) {
for (Entry<String, String> e : params.entrySet()) {
sb.append(e.getKey());
sb.append("=");
sb.append(e.getValue());
sb.append("&");
}
sb.substring(0, sb.length() - 1);
}
// 尝试发送请求
try {
u = new URL(url);
con = (HttpURLConnection) u.openConnection();
//// POST 只能为大写,严格限制,post会不识别
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setDoInput(true);
con.setUseCaches(false);
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream(), "UTF-8");
osw.write(sb.toString());
osw.flush();
osw.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (con != null) {
con.disconnect();
}
}
// 读取返回内容
StringBuffer buffer = new StringBuffer();
try {
//一定要有返回值,否则无法把请求发送给server端。
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
String temp;
while ((temp = br.readLine()) != null) {
buffer.append(temp);
buffer.append("\n");
}
} catch (Exception e) {
e.printStackTrace();
}
return buffer.toString();
}
public static void main(String[] args) {
JSONObject params = new JSONObject();
params.put("video_id", "60_8be004799c424688949704814ea0d16d");
params.put("state", 3 + "");
params.put("attr_tags", "");
params.put("msg", "");
HttpUtil httpUtil = HttpUtil.init();
httpUtil.setParam("param", params.toJSONString());
String URL = "http://mam.innerapi.cnlive.com/v1/inner/ShenHeInfo/shenheNotify?platform=AUDIT&token=2718c88930175686e40398994cead75c";
Map<String, String> post = httpUtil.post(URL);
System.out.println(post.get("result"));
}
}