HttpClientUtils.java
6.92 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
package com.qgutech.util;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.*;
import org.apache.http.client.config.RequestConfig;
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.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* http请求工具类
*/
@Slf4j
public class HttpClientUtils {
/**
* 发送post请求
*
* @return
*/
public static String doPostRequest(String url, Map<String, String> header, Map<String, String> params, HttpEntity httpEntity) {
String resultStr = "";
if (StringUtils.isEmpty(url)) {
return resultStr;
}
CloseableHttpClient httpClient = null;
CloseableHttpResponse httpResponse = null;
try {
httpClient = SSLClientCustom.getHttpClinet();
HttpPost httpPost = new HttpPost(url);
// 请求头header信息
if (MapUtils.isNotEmpty(header)) {
for (Map.Entry<String, String> stringStringEntry : header.entrySet()) {
httpPost.setHeader(stringStringEntry.getKey(), stringStringEntry.getValue());
}
}
// 请求参数信息
if (MapUtils.isNotEmpty(params)) {
List<NameValuePair> paramList = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> stringStringEntry : params.entrySet()) {
paramList.add(new BasicNameValuePair(stringStringEntry.getKey(), stringStringEntry.getValue()));
}
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(paramList, Consts.UTF_8);
httpPost.setEntity(urlEncodedFormEntity);
}
//实体设置
if (httpEntity != null) {
httpPost.setEntity(httpEntity);
}
//发起请求
httpResponse = httpClient.execute(httpPost);
if (null != httpResponse && null != httpResponse.getStatusLine()) {
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
HttpEntity httpResponseEntity = httpResponse.getEntity();
resultStr = EntityUtils.toString(httpResponseEntity);
} else {
StringBuffer stringBuffer = new StringBuffer();
HeaderIterator headerIterator = httpResponse.headerIterator();
while (headerIterator.hasNext()) {
stringBuffer.append("\t" + headerIterator.next());
}
log.info("异常信息:请求地址:{},响应状态和结果:{}", url, statusCode + ";" + stringBuffer);
}
} else {
log.error("请求异常,未获取正常相应,请求地址:{}", url);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
HttpClientUtils.closeConnection(httpClient, httpResponse);
}
return resultStr;
}
public static String doGetRequest(String url, Map<String, String> header, Map<String, String> params) {
String resultStr = "";
if (StringUtils.isEmpty(url)) {
return resultStr;
}
CloseableHttpClient httpClient = null;
CloseableHttpResponse httpResponse = null;
try {
httpClient = SSLClientCustom.getHttpClinet();
//请求参数信息
if (MapUtils.isNotEmpty(params)) {
url = url + buildUrl(params);
}
HttpGet httpGet = new HttpGet(url);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30000) //连接超时
.setConnectionRequestTimeout(30000) //请求超时
.setSocketTimeout(30000) //套接字连接超时
.setRedirectsEnabled(true).build(); //允许重定向
httpGet.setConfig(requestConfig);
if (MapUtils.isNotEmpty(header)) {
for (Map.Entry<String, String> stringStringEntry : header.entrySet()) {
httpGet.setHeader(stringStringEntry.getKey(), stringStringEntry.getValue());
}
}
//发起请求
httpResponse = httpClient.execute(httpGet);
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
resultStr = EntityUtils.toString(httpResponse.getEntity(), Consts.UTF_8);
} else {
StringBuffer stringBuffer = new StringBuffer();
HeaderIterator headerIterator = httpResponse.headerIterator();
while (headerIterator.hasNext()) {
stringBuffer.append("\t" + headerIterator.next());
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
HttpClientUtils.closeConnection(httpClient, httpResponse);
}
return resultStr;
}
/**
* 关掉连接释放资源
*/
private static void closeConnection(CloseableHttpClient httpClient, CloseableHttpResponse httpResponse) {
if (httpClient != null) {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (httpResponse != null) {
try {
httpResponse.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 构造get请求的参数
*
* @return
*/
private static String buildUrl(Map<String, String> map) {
if (MapUtils.isEmpty(map)) {
return "";
}
StringBuffer stringBuffer = new StringBuffer("?");
for (Map.Entry<String, String> stringStringEntry : map.entrySet()) {
try {
stringBuffer.append(URLEncoder.encode(stringStringEntry.getKey(), "UTF-8")).append("=");
stringBuffer.append(URLEncoder.encode(stringStringEntry.getValue(), "UTF-8")).append("&");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
String result = stringBuffer.toString();
if (StringUtils.isNotEmpty(result)) {
result = result.substring(0, result.length() - 1); //去掉结尾的&连接符
}
return result;
}
}