|
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+package com.srm.bpm.facde.oa.common;
|
|
|
2
|
+
|
|
|
3
|
+import org.apache.http.HeaderElement;
|
|
|
4
|
+import org.apache.http.HeaderElementIterator;
|
|
|
5
|
+import org.apache.http.HttpEntity;
|
|
|
6
|
+import org.apache.http.HttpResponse;
|
|
|
7
|
+import org.apache.http.client.config.RequestConfig;
|
|
|
8
|
+import org.apache.http.client.methods.*;
|
|
|
9
|
+import org.apache.http.client.protocol.HttpClientContext;
|
|
|
10
|
+import org.apache.http.config.Registry;
|
|
|
11
|
+import org.apache.http.config.RegistryBuilder;
|
|
|
12
|
+import org.apache.http.conn.ConnectionKeepAliveStrategy;
|
|
|
13
|
+import org.apache.http.conn.socket.ConnectionSocketFactory;
|
|
|
14
|
+import org.apache.http.conn.socket.PlainConnectionSocketFactory;
|
|
|
15
|
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
|
|
16
|
+import org.apache.http.entity.StringEntity;
|
|
|
17
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
18
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
19
|
+import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
|
|
20
|
+import org.apache.http.message.BasicHeaderElementIterator;
|
|
|
21
|
+import org.apache.http.protocol.HTTP;
|
|
|
22
|
+import org.apache.http.protocol.HttpContext;
|
|
|
23
|
+import org.apache.http.util.EntityUtils;
|
|
|
24
|
+
|
|
|
25
|
+import javax.net.ssl.SSLContext;
|
|
|
26
|
+import javax.net.ssl.TrustManager;
|
|
|
27
|
+import javax.net.ssl.X509TrustManager;
|
|
|
28
|
+import java.nio.charset.Charset;
|
|
|
29
|
+import java.security.KeyManagementException;
|
|
|
30
|
+import java.security.NoSuchAlgorithmException;
|
|
|
31
|
+import java.util.Map;
|
|
|
32
|
+import java.util.Map.Entry;
|
|
|
33
|
+
|
|
|
34
|
+/**
|
|
|
35
|
+ *
|
|
|
36
|
+ *<p>Title:HttpClientPoolUtil</p>
|
|
|
37
|
+ *<p>Description:客户端调用工具类</p>
|
|
|
38
|
+ *<p>Description:httpClient线程池工具类</p>
|
|
|
39
|
+ * @author zhangzhe
|
|
|
40
|
+ * @date 2021年04月23日
|
|
|
41
|
+ */
|
|
|
42
|
+public class HttpClientPoolUtil {
|
|
|
43
|
+
|
|
|
44
|
+ private static Class mc = HttpClientPoolUtil.class;
|
|
|
45
|
+ public static CloseableHttpClient httpClient = null;
|
|
|
46
|
+
|
|
|
47
|
+ /**
|
|
|
48
|
+ * 初始化连接池
|
|
|
49
|
+ * @throws NoSuchAlgorithmException
|
|
|
50
|
+ * @throws KeyManagementException
|
|
|
51
|
+ */
|
|
|
52
|
+ public static synchronized void initPools() throws KeyManagementException, NoSuchAlgorithmException {
|
|
|
53
|
+
|
|
|
54
|
+ if (httpClient == null) {
|
|
|
55
|
+ //采用绕过验证的方式处理https请求
|
|
|
56
|
+ SSLContext sslcontext = createIgnoreVerifySSL();
|
|
|
57
|
+ //设置协议http和https对应的处理socket链接工厂的对象
|
|
|
58
|
+ Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
|
|
|
59
|
+ .register("http", PlainConnectionSocketFactory.INSTANCE)
|
|
|
60
|
+ .register("https", new SSLConnectionSocketFactory(sslcontext,SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER))
|
|
|
61
|
+ .build();
|
|
|
62
|
+ PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
|
|
|
63
|
+ cm.setDefaultMaxPerRoute(20);
|
|
|
64
|
+ cm.setMaxTotal(500);
|
|
|
65
|
+ httpClient = HttpClients.custom().setKeepAliveStrategy(defaultStrategy).setConnectionManager(cm).build();
|
|
|
66
|
+ }
|
|
|
67
|
+ }
|
|
|
68
|
+
|
|
|
69
|
+ /**
|
|
|
70
|
+ * Http connection keepAlive 设置
|
|
|
71
|
+ */
|
|
|
72
|
+ public static ConnectionKeepAliveStrategy defaultStrategy = new ConnectionKeepAliveStrategy() {
|
|
|
73
|
+ public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
|
|
|
74
|
+ HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE));
|
|
|
75
|
+ int keepTime = 30;
|
|
|
76
|
+ while (it.hasNext()) {
|
|
|
77
|
+ HeaderElement he = it.nextElement();
|
|
|
78
|
+ String param = he.getName();
|
|
|
79
|
+ String value = he.getValue();
|
|
|
80
|
+ if (value != null && param.equalsIgnoreCase("timeout")) {
|
|
|
81
|
+ try {
|
|
|
82
|
+ return Long.parseLong(value) * 1000;
|
|
|
83
|
+ } catch (Exception e) {
|
|
|
84
|
+ e.printStackTrace();
|
|
|
85
|
+ }
|
|
|
86
|
+ }
|
|
|
87
|
+ }
|
|
|
88
|
+ return keepTime * 1000;
|
|
|
89
|
+ }
|
|
|
90
|
+ };
|
|
|
91
|
+
|
|
|
92
|
+ /**
|
|
|
93
|
+ * 绕过验证
|
|
|
94
|
+ *
|
|
|
95
|
+ * @return
|
|
|
96
|
+ * @throws NoSuchAlgorithmException
|
|
|
97
|
+ * @throws KeyManagementException
|
|
|
98
|
+ */
|
|
|
99
|
+ public static SSLContext createIgnoreVerifySSL() throws NoSuchAlgorithmException, KeyManagementException {
|
|
|
100
|
+ SSLContext sc = SSLContext.getInstance("SSLv3");
|
|
|
101
|
+
|
|
|
102
|
+ // 实现一个X509TrustManager接口,用于绕过验证,不用修改里面的方法
|
|
|
103
|
+ X509TrustManager trustManager = new X509TrustManager() {
|
|
|
104
|
+
|
|
|
105
|
+ public void checkClientTrusted(
|
|
|
106
|
+ java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
|
|
|
107
|
+ String paramString) {
|
|
|
108
|
+ }
|
|
|
109
|
+
|
|
|
110
|
+
|
|
|
111
|
+ public void checkServerTrusted(
|
|
|
112
|
+ java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
|
|
|
113
|
+ String paramString) {
|
|
|
114
|
+ }
|
|
|
115
|
+
|
|
|
116
|
+
|
|
|
117
|
+ public java.security.cert.X509Certificate[] getAcceptedIssuers() {
|
|
|
118
|
+ return null;
|
|
|
119
|
+ }
|
|
|
120
|
+ };
|
|
|
121
|
+
|
|
|
122
|
+ sc.init(null, new TrustManager[] { trustManager }, null);
|
|
|
123
|
+ return sc;
|
|
|
124
|
+ }
|
|
|
125
|
+
|
|
|
126
|
+ /**
|
|
|
127
|
+ * 创建请求
|
|
|
128
|
+ *
|
|
|
129
|
+ * @param url 请求url
|
|
|
130
|
+ * @param methodName 请求的方法类型
|
|
|
131
|
+ * @param headMap 请求头
|
|
|
132
|
+ * @return
|
|
|
133
|
+ * @throws NoSuchAlgorithmException
|
|
|
134
|
+ * @throws KeyManagementException
|
|
|
135
|
+ */
|
|
|
136
|
+ public static HttpRequestBase getRequest(String url, String methodName,Map<String, String> headMap)
|
|
|
137
|
+ throws KeyManagementException, NoSuchAlgorithmException {
|
|
|
138
|
+ if (httpClient == null) {
|
|
|
139
|
+ initPools();
|
|
|
140
|
+ }
|
|
|
141
|
+ HttpRequestBase method = null;
|
|
|
142
|
+ RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(30 * 1000).setConnectTimeout(30 * 1000)
|
|
|
143
|
+ .setConnectionRequestTimeout(30 * 1000).setExpectContinueEnabled(false).build();
|
|
|
144
|
+
|
|
|
145
|
+ if (HttpPut.METHOD_NAME.equalsIgnoreCase(methodName)) {
|
|
|
146
|
+ method = new HttpPut(url);
|
|
|
147
|
+ } else if (HttpPost.METHOD_NAME.equalsIgnoreCase(methodName)) {
|
|
|
148
|
+ method = new HttpPost(url);
|
|
|
149
|
+ } else if (HttpGet.METHOD_NAME.equalsIgnoreCase(methodName)) {
|
|
|
150
|
+ method = new HttpGet(url);
|
|
|
151
|
+ } else if (HttpDelete.METHOD_NAME.equalsIgnoreCase(methodName)) {
|
|
|
152
|
+ method = new HttpDelete(url);
|
|
|
153
|
+ } else {
|
|
|
154
|
+ method = new HttpPost(url);
|
|
|
155
|
+ }
|
|
|
156
|
+ if(!headMap.isEmpty()){
|
|
|
157
|
+ for(Entry<String, String> value:headMap.entrySet()){
|
|
|
158
|
+ method.addHeader(value.getKey(), value.getValue());
|
|
|
159
|
+ }
|
|
|
160
|
+ }
|
|
|
161
|
+ method.setConfig(requestConfig);
|
|
|
162
|
+ return method;
|
|
|
163
|
+ }
|
|
|
164
|
+
|
|
|
165
|
+
|
|
|
166
|
+ /**
|
|
|
167
|
+ * 执行GET 请求
|
|
|
168
|
+ *
|
|
|
169
|
+ * @param url
|
|
|
170
|
+ * @return
|
|
|
171
|
+ */
|
|
|
172
|
+ public static String get(String url,Map<String, String> headMap) throws Exception{
|
|
|
173
|
+ HttpEntity httpEntity = null;
|
|
|
174
|
+ HttpRequestBase method = null;
|
|
|
175
|
+ String responseBody = "";
|
|
|
176
|
+ try {
|
|
|
177
|
+ if (httpClient == null) {
|
|
|
178
|
+ initPools();
|
|
|
179
|
+ }
|
|
|
180
|
+ method = getRequest(url, HttpGet.METHOD_NAME,headMap);
|
|
|
181
|
+ HttpContext context = HttpClientContext.create();
|
|
|
182
|
+ CloseableHttpResponse httpResponse = httpClient.execute(method, context);
|
|
|
183
|
+ httpEntity = httpResponse.getEntity();
|
|
|
184
|
+ if (httpEntity != null) {
|
|
|
185
|
+ responseBody = EntityUtils.toString(httpEntity, "UTF-8");
|
|
|
186
|
+ }
|
|
|
187
|
+ } catch (Exception e) {
|
|
|
188
|
+ if(method != null){
|
|
|
189
|
+ method.abort();
|
|
|
190
|
+ }
|
|
|
191
|
+ throw e;
|
|
|
192
|
+ } finally {
|
|
|
193
|
+ if (httpEntity != null) {
|
|
|
194
|
+ try {
|
|
|
195
|
+ EntityUtils.consumeQuietly(httpEntity);
|
|
|
196
|
+ } catch (Exception e) {
|
|
|
197
|
+ throw e;
|
|
|
198
|
+ }
|
|
|
199
|
+ }
|
|
|
200
|
+ }
|
|
|
201
|
+ return responseBody;
|
|
|
202
|
+ }
|
|
|
203
|
+
|
|
|
204
|
+ /**
|
|
|
205
|
+ * 执行http post请求 默认采用Content-Type:application/json,Accept:application/json
|
|
|
206
|
+ *
|
|
|
207
|
+ * @param url 请求地址
|
|
|
208
|
+ * @param data 请求数据
|
|
|
209
|
+ * @param data 请求头
|
|
|
210
|
+ * @return
|
|
|
211
|
+ */
|
|
|
212
|
+ public static String post(String url, String data,Map<String, String> headMap) throws Exception{
|
|
|
213
|
+ HttpEntity httpEntity = null;
|
|
|
214
|
+ HttpEntityEnclosingRequestBase method = null;
|
|
|
215
|
+ String responseBody = "";
|
|
|
216
|
+ try {
|
|
|
217
|
+ if (httpClient == null) {
|
|
|
218
|
+ initPools();
|
|
|
219
|
+ }
|
|
|
220
|
+ method = (HttpEntityEnclosingRequestBase) getRequest(url, HttpPost.METHOD_NAME,headMap);
|
|
|
221
|
+ method.setEntity(new StringEntity(data,Charset.forName("UTF-8")));
|
|
|
222
|
+ HttpContext context = HttpClientContext.create();
|
|
|
223
|
+ CloseableHttpResponse httpResponse = httpClient.execute(method, context);
|
|
|
224
|
+// ZcLogs.WriteLog(mc, "httpResponse:"+httpResponse.toString());
|
|
|
225
|
+ httpEntity = httpResponse.getEntity();
|
|
|
226
|
+ if (httpEntity != null) {
|
|
|
227
|
+ responseBody = EntityUtils.toString(httpEntity, "UTF-8");
|
|
|
228
|
+ }
|
|
|
229
|
+
|
|
|
230
|
+ } catch (Exception e) {
|
|
|
231
|
+ if(method != null){
|
|
|
232
|
+ method.abort();
|
|
|
233
|
+ }
|
|
|
234
|
+ throw e;
|
|
|
235
|
+ } finally {
|
|
|
236
|
+ if (httpEntity != null) {
|
|
|
237
|
+ try {
|
|
|
238
|
+ EntityUtils.consumeQuietly(httpEntity);
|
|
|
239
|
+ } catch (Exception e) {
|
|
|
240
|
+ throw e;
|
|
|
241
|
+ }
|
|
|
242
|
+ }
|
|
|
243
|
+ }
|
|
|
244
|
+ return responseBody;
|
|
|
245
|
+ }
|
|
|
246
|
+
|
|
|
247
|
+ /**
|
|
|
248
|
+ * 执行http put请求 默认采用Content-Type:application/json,Accept:application/json
|
|
|
249
|
+ *
|
|
|
250
|
+ * @param url 请求地址
|
|
|
251
|
+ * @param data 请求数据
|
|
|
252
|
+ * @param data 请求头
|
|
|
253
|
+ * @return
|
|
|
254
|
+ */
|
|
|
255
|
+ public static String put(String url, String data,Map<String, String> headMap) throws Exception{
|
|
|
256
|
+ HttpEntity httpEntity = null;
|
|
|
257
|
+ HttpEntityEnclosingRequestBase method = null;
|
|
|
258
|
+ String responseBody = "";
|
|
|
259
|
+ try {
|
|
|
260
|
+ if (httpClient == null) {
|
|
|
261
|
+ initPools();
|
|
|
262
|
+ }
|
|
|
263
|
+ method = (HttpEntityEnclosingRequestBase) getRequest(url, HttpPut.METHOD_NAME,headMap);
|
|
|
264
|
+ method.setEntity(new StringEntity(data,Charset.forName("UTF-8")));
|
|
|
265
|
+ HttpContext context = HttpClientContext.create();
|
|
|
266
|
+ CloseableHttpResponse httpResponse = httpClient.execute(method, context);
|
|
|
267
|
+ httpEntity = httpResponse.getEntity();
|
|
|
268
|
+ if (httpEntity != null) {
|
|
|
269
|
+ responseBody = EntityUtils.toString(httpEntity, "UTF-8");
|
|
|
270
|
+ }
|
|
|
271
|
+
|
|
|
272
|
+ } catch (Exception e) {
|
|
|
273
|
+ if(method != null){
|
|
|
274
|
+ method.abort();
|
|
|
275
|
+ }
|
|
|
276
|
+ throw e;
|
|
|
277
|
+ } finally {
|
|
|
278
|
+ if (httpEntity != null) {
|
|
|
279
|
+ try {
|
|
|
280
|
+ EntityUtils.consumeQuietly(httpEntity);
|
|
|
281
|
+ } catch (Exception e) {
|
|
|
282
|
+ throw e;
|
|
|
283
|
+ }
|
|
|
284
|
+ }
|
|
|
285
|
+ }
|
|
|
286
|
+ return responseBody;
|
|
|
287
|
+ }
|
|
|
288
|
+
|
|
|
289
|
+ /**
|
|
|
290
|
+ * 执行DELETE 请求
|
|
|
291
|
+ *
|
|
|
292
|
+ * @param url
|
|
|
293
|
+ * @return
|
|
|
294
|
+ */
|
|
|
295
|
+ public static String delete(String url,Map<String, String> headMap) throws Exception{
|
|
|
296
|
+ HttpEntity httpEntity = null;
|
|
|
297
|
+ HttpRequestBase method = null;
|
|
|
298
|
+ String responseBody = "";
|
|
|
299
|
+ try {
|
|
|
300
|
+ if (httpClient == null) {
|
|
|
301
|
+ initPools();
|
|
|
302
|
+ }
|
|
|
303
|
+ method = getRequest(url, HttpDelete.METHOD_NAME,headMap);
|
|
|
304
|
+ HttpContext context = HttpClientContext.create();
|
|
|
305
|
+ CloseableHttpResponse httpResponse = httpClient.execute(method, context);
|
|
|
306
|
+ httpEntity = httpResponse.getEntity();
|
|
|
307
|
+ if (httpEntity != null) {
|
|
|
308
|
+ responseBody = EntityUtils.toString(httpEntity, "UTF-8");
|
|
|
309
|
+ }
|
|
|
310
|
+ } catch (Exception e) {
|
|
|
311
|
+ if(method != null){
|
|
|
312
|
+ method.abort();
|
|
|
313
|
+ }
|
|
|
314
|
+ throw e;
|
|
|
315
|
+ } finally {
|
|
|
316
|
+ if (httpEntity != null) {
|
|
|
317
|
+ try {
|
|
|
318
|
+ EntityUtils.consumeQuietly(httpEntity);
|
|
|
319
|
+ } catch (Exception e) {
|
|
|
320
|
+ throw e;
|
|
|
321
|
+ }
|
|
|
322
|
+ }
|
|
|
323
|
+ }
|
|
|
324
|
+ return responseBody;
|
|
|
325
|
+ }
|
|
|
326
|
+
|
|
|
327
|
+}
|