|
|
@@ -0,0 +1,486 @@
|
|
|
1
|
+/*
|
|
|
2
|
+ *
|
|
|
3
|
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
|
|
4
|
+ * the License. You may obtain a copy of the License at
|
|
|
5
|
+ *
|
|
|
6
|
+ * http://www.apache.org/licenses/LICENSE-2.0
|
|
|
7
|
+ *
|
|
|
8
|
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
|
|
9
|
+ * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
|
10
|
+ * specific language governing permissions and limitations under the License.
|
|
|
11
|
+ */
|
|
|
12
|
+package com.ruoyi.common.constant;
|
|
|
13
|
+import com.ruoyi.common.core.domain.entity.EsignHttpResponse;
|
|
|
14
|
+import com.ruoyi.common.enums.EsignRequestType;
|
|
|
15
|
+import com.ruoyi.common.exception.EsignDemoException;
|
|
|
16
|
+import org.apache.http.*;
|
|
|
17
|
+import org.apache.http.auth.AuthScope;
|
|
|
18
|
+import org.apache.http.auth.UsernamePasswordCredentials;
|
|
|
19
|
+import org.apache.http.client.ClientProtocolException;
|
|
|
20
|
+import org.apache.http.client.CredentialsProvider;
|
|
|
21
|
+import org.apache.http.client.HttpRequestRetryHandler;
|
|
|
22
|
+import org.apache.http.client.config.RequestConfig;
|
|
|
23
|
+import org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
|
24
|
+import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
25
|
+import org.apache.http.client.methods.HttpRequestBase;
|
|
|
26
|
+import org.apache.http.client.protocol.HttpClientContext;
|
|
|
27
|
+import org.apache.http.client.utils.URIBuilder;
|
|
|
28
|
+import org.apache.http.config.Registry;
|
|
|
29
|
+import org.apache.http.config.RegistryBuilder;
|
|
|
30
|
+import org.apache.http.conn.ConnectTimeoutException;
|
|
|
31
|
+import org.apache.http.conn.socket.ConnectionSocketFactory;
|
|
|
32
|
+import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
|
|
|
33
|
+import org.apache.http.conn.socket.PlainConnectionSocketFactory;
|
|
|
34
|
+import org.apache.http.conn.ssl.NoopHostnameVerifier;
|
|
|
35
|
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
|
|
36
|
+import org.apache.http.entity.ByteArrayEntity;
|
|
|
37
|
+import org.apache.http.entity.ContentType;
|
|
|
38
|
+import org.apache.http.entity.StringEntity;
|
|
|
39
|
+import org.apache.http.impl.client.BasicCredentialsProvider;
|
|
|
40
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
41
|
+import org.apache.http.impl.client.HttpClientBuilder;
|
|
|
42
|
+import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
|
|
43
|
+import org.apache.http.message.BasicNameValuePair;
|
|
|
44
|
+import org.apache.http.protocol.HttpContext;
|
|
|
45
|
+import org.apache.http.util.EntityUtils;
|
|
|
46
|
+import org.slf4j.Logger;
|
|
|
47
|
+import org.slf4j.LoggerFactory;
|
|
|
48
|
+
|
|
|
49
|
+import javax.net.ssl.*;
|
|
|
50
|
+import java.io.IOException;
|
|
|
51
|
+import java.io.InterruptedIOException;
|
|
|
52
|
+import java.net.UnknownHostException;
|
|
|
53
|
+import java.security.cert.CertificateException;
|
|
|
54
|
+import java.security.cert.X509Certificate;
|
|
|
55
|
+import java.text.MessageFormat;
|
|
|
56
|
+import java.util.ArrayList;
|
|
|
57
|
+import java.util.Arrays;
|
|
|
58
|
+import java.util.List;
|
|
|
59
|
+import java.util.Map;
|
|
|
60
|
+
|
|
|
61
|
+/**
|
|
|
62
|
+ * @description Http请求 辅助类
|
|
|
63
|
+ * @author 澄泓
|
|
|
64
|
+ * @since JDK1.7
|
|
|
65
|
+ */
|
|
|
66
|
+public class EsignHttpCfgHelper {
|
|
|
67
|
+
|
|
|
68
|
+ private static Logger LOGGER = LoggerFactory.getLogger(EsignHttpCfgHelper.class);
|
|
|
69
|
+ /**
|
|
|
70
|
+ * 超时时间,默认15000毫秒
|
|
|
71
|
+ */
|
|
|
72
|
+ private static int MAX_TIMEOUT = 15000;
|
|
|
73
|
+ /**
|
|
|
74
|
+ * 请求池最大连接数,默认100个
|
|
|
75
|
+ */
|
|
|
76
|
+ private static int MAX_TOTAL=100;
|
|
|
77
|
+ /**
|
|
|
78
|
+ * 单域名最大的连接数,默认50个
|
|
|
79
|
+ */
|
|
|
80
|
+ private static int ROUTE_MAX_TOTAL=50;
|
|
|
81
|
+ /**
|
|
|
82
|
+ * 请求失败重试次数,默认3次
|
|
|
83
|
+ */
|
|
|
84
|
+ private static int MAX_RETRY = 3;
|
|
|
85
|
+ /**
|
|
|
86
|
+ * 是否需要域名校验,默认不需要校验
|
|
|
87
|
+ */
|
|
|
88
|
+ private static boolean SSL_VERIFY=false;
|
|
|
89
|
+
|
|
|
90
|
+ /**
|
|
|
91
|
+ * 正向代理IP
|
|
|
92
|
+ */
|
|
|
93
|
+ private static String PROXY_IP;
|
|
|
94
|
+ /**
|
|
|
95
|
+ * 正向代理端口,默认8888
|
|
|
96
|
+ */
|
|
|
97
|
+ private static int PROXY_PORT=8888;
|
|
|
98
|
+ /**
|
|
|
99
|
+ * 代理协议,默认http
|
|
|
100
|
+ */
|
|
|
101
|
+ private static String PROXY_AGREEMENT="http";
|
|
|
102
|
+
|
|
|
103
|
+ /**
|
|
|
104
|
+ * 是否开启代理,默认false
|
|
|
105
|
+ */
|
|
|
106
|
+ private static boolean OPEN_PROXY=false;
|
|
|
107
|
+
|
|
|
108
|
+ /**
|
|
|
109
|
+ * 代理服务器用户名
|
|
|
110
|
+ */
|
|
|
111
|
+ private static String PROXY_USERNAME="";
|
|
|
112
|
+
|
|
|
113
|
+ /**
|
|
|
114
|
+ * 代理服务器密码
|
|
|
115
|
+ */
|
|
|
116
|
+ private static String PROXY_PASSWORD="";
|
|
|
117
|
+
|
|
|
118
|
+
|
|
|
119
|
+ private static PoolingHttpClientConnectionManager connMgr; //连接池
|
|
|
120
|
+ private static HttpRequestRetryHandler retryHandler; //重试机制
|
|
|
121
|
+
|
|
|
122
|
+ private static CloseableHttpClient httpClient=null;
|
|
|
123
|
+
|
|
|
124
|
+ public static int getMaxTimeout() {
|
|
|
125
|
+ return MAX_TIMEOUT;
|
|
|
126
|
+ }
|
|
|
127
|
+
|
|
|
128
|
+ public static void setMaxTimeout(int maxTimeout) {
|
|
|
129
|
+ MAX_TIMEOUT = maxTimeout;
|
|
|
130
|
+ }
|
|
|
131
|
+
|
|
|
132
|
+ public static int getMaxTotal() {
|
|
|
133
|
+ return MAX_TOTAL;
|
|
|
134
|
+ }
|
|
|
135
|
+
|
|
|
136
|
+ public static void setMaxTotal(int maxTotal) {
|
|
|
137
|
+ MAX_TOTAL = maxTotal;
|
|
|
138
|
+ }
|
|
|
139
|
+
|
|
|
140
|
+ public static int getRouteMaxTotal() {
|
|
|
141
|
+ return ROUTE_MAX_TOTAL;
|
|
|
142
|
+ }
|
|
|
143
|
+
|
|
|
144
|
+ public static void setRouteMaxTotal(int routeMaxTotal) {
|
|
|
145
|
+ ROUTE_MAX_TOTAL = routeMaxTotal;
|
|
|
146
|
+ }
|
|
|
147
|
+
|
|
|
148
|
+ public static int getMaxRetry() {
|
|
|
149
|
+ return MAX_RETRY;
|
|
|
150
|
+ }
|
|
|
151
|
+
|
|
|
152
|
+ public static void setMaxRetry(int maxRetry) {
|
|
|
153
|
+ MAX_RETRY = maxRetry;
|
|
|
154
|
+ }
|
|
|
155
|
+
|
|
|
156
|
+ public static boolean isSslVerify() {
|
|
|
157
|
+ return SSL_VERIFY;
|
|
|
158
|
+ }
|
|
|
159
|
+
|
|
|
160
|
+ public static void setSslVerify(boolean sslVerify) {
|
|
|
161
|
+ SSL_VERIFY = sslVerify;
|
|
|
162
|
+ }
|
|
|
163
|
+
|
|
|
164
|
+ public static String getProxyIp() {
|
|
|
165
|
+ return PROXY_IP;
|
|
|
166
|
+ }
|
|
|
167
|
+
|
|
|
168
|
+ public static void setProxyIp(String proxyIp) {
|
|
|
169
|
+ PROXY_IP = proxyIp;
|
|
|
170
|
+ }
|
|
|
171
|
+
|
|
|
172
|
+ public static int getProxyPort() {
|
|
|
173
|
+ return PROXY_PORT;
|
|
|
174
|
+ }
|
|
|
175
|
+
|
|
|
176
|
+ public static void setProxyPort(int proxyPort) {
|
|
|
177
|
+ PROXY_PORT = proxyPort;
|
|
|
178
|
+ }
|
|
|
179
|
+
|
|
|
180
|
+ public static String getProxyAgreement() {
|
|
|
181
|
+ return PROXY_AGREEMENT;
|
|
|
182
|
+ }
|
|
|
183
|
+
|
|
|
184
|
+ public static void setProxyAgreement(String proxyAgreement) {
|
|
|
185
|
+ PROXY_AGREEMENT = proxyAgreement;
|
|
|
186
|
+ }
|
|
|
187
|
+
|
|
|
188
|
+ public static boolean getOpenProxy() {
|
|
|
189
|
+ return OPEN_PROXY;
|
|
|
190
|
+ }
|
|
|
191
|
+
|
|
|
192
|
+ public static void setOpenProxy(boolean openProxy) {
|
|
|
193
|
+ OPEN_PROXY = openProxy;
|
|
|
194
|
+ }
|
|
|
195
|
+
|
|
|
196
|
+ public static String getProxyUsername() {
|
|
|
197
|
+ return PROXY_USERNAME;
|
|
|
198
|
+ }
|
|
|
199
|
+
|
|
|
200
|
+ public static void setProxyUserame(String proxyUsername) {
|
|
|
201
|
+ PROXY_USERNAME = proxyUsername;
|
|
|
202
|
+ }
|
|
|
203
|
+
|
|
|
204
|
+ public static String getProxyPassword() {
|
|
|
205
|
+ return PROXY_PASSWORD;
|
|
|
206
|
+ }
|
|
|
207
|
+
|
|
|
208
|
+ public static void setProxyPassword(String proxyPassword) {
|
|
|
209
|
+ PROXY_PASSWORD = proxyPassword;
|
|
|
210
|
+ }
|
|
|
211
|
+
|
|
|
212
|
+
|
|
|
213
|
+
|
|
|
214
|
+
|
|
|
215
|
+ /**
|
|
|
216
|
+ * 不允许外部创建实例
|
|
|
217
|
+ */
|
|
|
218
|
+ private EsignHttpCfgHelper() {
|
|
|
219
|
+ }
|
|
|
220
|
+
|
|
|
221
|
+ //------------------------------公有方法start--------------------------------------------
|
|
|
222
|
+
|
|
|
223
|
+
|
|
|
224
|
+ /**
|
|
|
225
|
+ * @description 发起HTTP / HTTPS 请求
|
|
|
226
|
+ *
|
|
|
227
|
+ * @param reqType
|
|
|
228
|
+ * {@link EsignRequestType} 请求类型 GET、 POST 、 DELETE 、 PUT
|
|
|
229
|
+ * @param httpUrl
|
|
|
230
|
+ * {@link String} 请求目标地址
|
|
|
231
|
+ * @param headers
|
|
|
232
|
+ * {@link Map} 请求头
|
|
|
233
|
+ * @param param
|
|
|
234
|
+ * {@link Object} 参数
|
|
|
235
|
+ * @return
|
|
|
236
|
+ * @throws EsignDemoException
|
|
|
237
|
+ * @author 澄泓
|
|
|
238
|
+ */
|
|
|
239
|
+ public static EsignHttpResponse sendHttp(EsignRequestType reqType, String httpUrl, Map<String, String> headers, Object param, boolean debug)
|
|
|
240
|
+ throws EsignDemoException {
|
|
|
241
|
+ HttpRequestBase reqBase=null;
|
|
|
242
|
+ if(httpUrl.startsWith("http")){
|
|
|
243
|
+ reqBase=reqType.getHttpType(httpUrl);
|
|
|
244
|
+ }else{
|
|
|
245
|
+ throw new EsignDemoException("请求url地址格式错误");
|
|
|
246
|
+ }
|
|
|
247
|
+ if(debug){
|
|
|
248
|
+ LOGGER.info("请求头:{}",headers+"\n");
|
|
|
249
|
+ LOGGER.info("请求参数\n{}", param+"\n");
|
|
|
250
|
+ LOGGER.info("请求地址\n:{}\n请求方式\n:{}",reqBase.getURI(),reqType+"\n");
|
|
|
251
|
+ }
|
|
|
252
|
+ //请求方法不是GET或者DELETE时传入body体,否则不传入。
|
|
|
253
|
+ String[] methods = {"DELETE", "GET"};
|
|
|
254
|
+ if(param instanceof String&&Arrays.binarySearch(methods, reqType.name())<0){//POST或者PUT请求
|
|
|
255
|
+ ((HttpEntityEnclosingRequest) reqBase).setEntity(
|
|
|
256
|
+ new StringEntity(String.valueOf(param), ContentType.create("application/json", "UTF-8")));
|
|
|
257
|
+ }
|
|
|
258
|
+ //参数时字节流数组
|
|
|
259
|
+ else if(param instanceof byte[]) {
|
|
|
260
|
+ reqBase=reqType.getHttpType(httpUrl);
|
|
|
261
|
+ byte[] paramBytes = (byte[])param;
|
|
|
262
|
+ ((HttpEntityEnclosingRequest) reqBase).setEntity(new ByteArrayEntity(paramBytes));
|
|
|
263
|
+ }
|
|
|
264
|
+ //参数是form表单时
|
|
|
265
|
+ else if(param instanceof List){
|
|
|
266
|
+ ((HttpEntityEnclosingRequest) reqBase).setEntity(new UrlEncodedFormEntity((Iterable<? extends NameValuePair>) param));
|
|
|
267
|
+ }
|
|
|
268
|
+ httpClient = getHttpClient();
|
|
|
269
|
+ config(reqBase);
|
|
|
270
|
+
|
|
|
271
|
+ //设置请求头
|
|
|
272
|
+ if(headers != null &&headers.size()>0) {
|
|
|
273
|
+ for(Map.Entry<String, String> entry :headers.entrySet()) {
|
|
|
274
|
+ reqBase.setHeader(entry.getKey(), entry.getValue());
|
|
|
275
|
+ }
|
|
|
276
|
+ }
|
|
|
277
|
+ //响应对象
|
|
|
278
|
+ CloseableHttpResponse res = null;
|
|
|
279
|
+ //响应内容
|
|
|
280
|
+ String resCtx = null;
|
|
|
281
|
+ int status;
|
|
|
282
|
+ EsignHttpResponse esignHttpResponse = new EsignHttpResponse();
|
|
|
283
|
+ try {
|
|
|
284
|
+ //执行请求
|
|
|
285
|
+ res = httpClient.execute(reqBase);
|
|
|
286
|
+ status=res.getStatusLine().getStatusCode();
|
|
|
287
|
+
|
|
|
288
|
+ //获取请求响应对象和响应entity
|
|
|
289
|
+ HttpEntity httpEntity = res.getEntity();
|
|
|
290
|
+ if(httpEntity != null) {
|
|
|
291
|
+ resCtx = EntityUtils.toString(httpEntity,"utf-8");
|
|
|
292
|
+ }
|
|
|
293
|
+ if(debug) {
|
|
|
294
|
+ LOGGER.info("响应\n{}", resCtx + "\n");
|
|
|
295
|
+ LOGGER.info("----------------------------end------------------------");
|
|
|
296
|
+ }
|
|
|
297
|
+ } catch (NoHttpResponseException e) {
|
|
|
298
|
+ throw new EsignDemoException("服务器丢失了",e);
|
|
|
299
|
+ } catch (SSLHandshakeException e){
|
|
|
300
|
+ String msg = MessageFormat.format("SSL握手异常", e);
|
|
|
301
|
+ EsignDemoException ex = new EsignDemoException(msg, e);
|
|
|
302
|
+ throw ex;
|
|
|
303
|
+ } catch (UnknownHostException e){
|
|
|
304
|
+ EsignDemoException ex = new EsignDemoException("服务器找不到", e);
|
|
|
305
|
+ ex.initCause(e);
|
|
|
306
|
+ throw ex;
|
|
|
307
|
+ } catch(ConnectTimeoutException e){
|
|
|
308
|
+ EsignDemoException ex = new EsignDemoException("连接超时", e);
|
|
|
309
|
+ ex.initCause(e);
|
|
|
310
|
+ throw ex;
|
|
|
311
|
+ } catch(SSLException e){
|
|
|
312
|
+ EsignDemoException ex = new EsignDemoException("SSL异常",e);
|
|
|
313
|
+ ex.initCause(e);
|
|
|
314
|
+ throw ex;
|
|
|
315
|
+ } catch (ClientProtocolException e) {
|
|
|
316
|
+ EsignDemoException ex = new EsignDemoException("请求头异常",e);
|
|
|
317
|
+ ex.initCause(e);
|
|
|
318
|
+ throw ex;
|
|
|
319
|
+ } catch (IOException e) {
|
|
|
320
|
+ EsignDemoException ex = new EsignDemoException("网络请求失败",e);
|
|
|
321
|
+ ex.initCause(e);
|
|
|
322
|
+ throw ex;
|
|
|
323
|
+ } finally {
|
|
|
324
|
+ if(res != null) {
|
|
|
325
|
+ try {
|
|
|
326
|
+ res.close();
|
|
|
327
|
+ } catch (IOException e) {
|
|
|
328
|
+ EsignDemoException ex = new EsignDemoException("--->>关闭请求响应失败",e);
|
|
|
329
|
+ ex.initCause(e);
|
|
|
330
|
+ throw ex;
|
|
|
331
|
+ }
|
|
|
332
|
+ }
|
|
|
333
|
+ }
|
|
|
334
|
+ esignHttpResponse.setStatus(status);
|
|
|
335
|
+ esignHttpResponse.setBody(resCtx);
|
|
|
336
|
+ return esignHttpResponse;
|
|
|
337
|
+ }
|
|
|
338
|
+ //------------------------------公有方法end----------------------------------------------
|
|
|
339
|
+
|
|
|
340
|
+ //------------------------------私有方法start--------------------------------------------
|
|
|
341
|
+
|
|
|
342
|
+ /**
|
|
|
343
|
+ * @description 请求头和超时时间配置
|
|
|
344
|
+ *
|
|
|
345
|
+ * @param httpReqBase
|
|
|
346
|
+ * @author 澄泓
|
|
|
347
|
+ */
|
|
|
348
|
+ private static void config(HttpRequestBase httpReqBase) {
|
|
|
349
|
+ // 配置请求的超时设置
|
|
|
350
|
+ RequestConfig.Builder builder = RequestConfig.custom()
|
|
|
351
|
+ .setConnectionRequestTimeout(MAX_TIMEOUT)
|
|
|
352
|
+ .setConnectTimeout(MAX_TIMEOUT)
|
|
|
353
|
+ .setSocketTimeout(MAX_TIMEOUT);
|
|
|
354
|
+ if(OPEN_PROXY){
|
|
|
355
|
+ HttpHost proxy=new HttpHost(PROXY_IP,PROXY_PORT,PROXY_AGREEMENT);
|
|
|
356
|
+ builder.setProxy(proxy);
|
|
|
357
|
+ }
|
|
|
358
|
+ RequestConfig requestConfig = builder.build();
|
|
|
359
|
+ httpReqBase.setConfig(requestConfig);
|
|
|
360
|
+ }
|
|
|
361
|
+
|
|
|
362
|
+ /**
|
|
|
363
|
+ * @description 连接池配置
|
|
|
364
|
+ *
|
|
|
365
|
+ * @return
|
|
|
366
|
+ * @author 澄泓
|
|
|
367
|
+ */
|
|
|
368
|
+ private static void cfgPoolMgr() throws EsignDemoException {
|
|
|
369
|
+ ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory();
|
|
|
370
|
+ LayeredConnectionSocketFactory sslsf = SSLConnectionSocketFactory.getSocketFactory();
|
|
|
371
|
+ if(!SSL_VERIFY){
|
|
|
372
|
+ sslsf=sslConnectionSocketFactory();
|
|
|
373
|
+ }
|
|
|
374
|
+
|
|
|
375
|
+ Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
|
|
|
376
|
+ .register("http", plainsf)
|
|
|
377
|
+ .register("https", sslsf)
|
|
|
378
|
+ .build();
|
|
|
379
|
+
|
|
|
380
|
+ //连接池管理器
|
|
|
381
|
+ connMgr = new PoolingHttpClientConnectionManager(registry);
|
|
|
382
|
+ //请求池最大连接数
|
|
|
383
|
+ connMgr.setMaxTotal(MAX_TOTAL);
|
|
|
384
|
+ //但域名最大的连接数
|
|
|
385
|
+ connMgr.setDefaultMaxPerRoute(ROUTE_MAX_TOTAL);
|
|
|
386
|
+ }
|
|
|
387
|
+
|
|
|
388
|
+
|
|
|
389
|
+
|
|
|
390
|
+
|
|
|
391
|
+ /**
|
|
|
392
|
+ * @description 设置重试机制
|
|
|
393
|
+ *
|
|
|
394
|
+ * @author 澄泓
|
|
|
395
|
+ */
|
|
|
396
|
+ private static void cfgRetryHandler() {
|
|
|
397
|
+ retryHandler = new HttpRequestRetryHandler() {
|
|
|
398
|
+
|
|
|
399
|
+ @Override
|
|
|
400
|
+ public boolean retryRequest(IOException e, int excCount, HttpContext ctx) {
|
|
|
401
|
+ //超过最大重试次数,就放弃
|
|
|
402
|
+ if(excCount > MAX_RETRY) {
|
|
|
403
|
+ return false;
|
|
|
404
|
+ }
|
|
|
405
|
+ //服务器丢掉了链接,就重试
|
|
|
406
|
+ if(e instanceof NoHttpResponseException) {
|
|
|
407
|
+ return true;
|
|
|
408
|
+ }
|
|
|
409
|
+ //不重试SSL握手异常
|
|
|
410
|
+ if(e instanceof SSLHandshakeException) {
|
|
|
411
|
+ return false;
|
|
|
412
|
+ }
|
|
|
413
|
+ //中断
|
|
|
414
|
+ if(e instanceof InterruptedIOException) {
|
|
|
415
|
+ return false;
|
|
|
416
|
+ }
|
|
|
417
|
+ //目标服务器不可达
|
|
|
418
|
+ if(e instanceof UnknownHostException) {
|
|
|
419
|
+ return false;
|
|
|
420
|
+ }
|
|
|
421
|
+ //连接超时
|
|
|
422
|
+ //SSL异常
|
|
|
423
|
+ if(e instanceof SSLException) {
|
|
|
424
|
+ return false;
|
|
|
425
|
+ }
|
|
|
426
|
+
|
|
|
427
|
+ HttpClientContext clientCtx = HttpClientContext.adapt(ctx);
|
|
|
428
|
+ HttpRequest req = clientCtx.getRequest();
|
|
|
429
|
+ //如果是幂等请求,就再次尝试
|
|
|
430
|
+ if(!(req instanceof HttpEntityEnclosingRequest)) {
|
|
|
431
|
+ return true;
|
|
|
432
|
+ }
|
|
|
433
|
+ return false;
|
|
|
434
|
+ }
|
|
|
435
|
+ };
|
|
|
436
|
+ }
|
|
|
437
|
+
|
|
|
438
|
+ /**
|
|
|
439
|
+ * 忽略域名校验
|
|
|
440
|
+ */
|
|
|
441
|
+ private static SSLConnectionSocketFactory sslConnectionSocketFactory() throws EsignDemoException {
|
|
|
442
|
+ try {
|
|
|
443
|
+ SSLContext ctx = SSLContext.getInstance("TLS"); // 创建一个上下文(此处指定的协议类型似乎不是重点)
|
|
|
444
|
+ X509TrustManager tm = new X509TrustManager() { // 创建一个跳过SSL证书的策略
|
|
|
445
|
+ public X509Certificate[] getAcceptedIssuers() {
|
|
|
446
|
+ return null;
|
|
|
447
|
+ }
|
|
|
448
|
+
|
|
|
449
|
+ public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
|
|
|
450
|
+ }
|
|
|
451
|
+
|
|
|
452
|
+ public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
|
|
|
453
|
+ }
|
|
|
454
|
+ };
|
|
|
455
|
+ ctx.init(null, new TrustManager[] { tm }, null); // 使用上面的策略初始化上下文
|
|
|
456
|
+ return new SSLConnectionSocketFactory(ctx, new String[] { "SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2" }, null, NoopHostnameVerifier.INSTANCE);
|
|
|
457
|
+ }catch (Exception e){
|
|
|
458
|
+ EsignDemoException ex = new EsignDemoException("忽略域名校验失败",e);
|
|
|
459
|
+ ex.initCause(e);
|
|
|
460
|
+ throw ex;
|
|
|
461
|
+ }
|
|
|
462
|
+
|
|
|
463
|
+ }
|
|
|
464
|
+
|
|
|
465
|
+ /**
|
|
|
466
|
+ * @description 获取单例HttpClient
|
|
|
467
|
+ *
|
|
|
468
|
+ * @return
|
|
|
469
|
+ * @author 澄泓
|
|
|
470
|
+ */
|
|
|
471
|
+ private static synchronized CloseableHttpClient getHttpClient() throws EsignDemoException {
|
|
|
472
|
+ if(httpClient==null) {
|
|
|
473
|
+ CredentialsProvider credsProvider = new BasicCredentialsProvider();
|
|
|
474
|
+ credsProvider.setCredentials(new AuthScope(PROXY_IP,PROXY_PORT),new UsernamePasswordCredentials(PROXY_USERNAME, PROXY_PASSWORD));
|
|
|
475
|
+ cfgPoolMgr();
|
|
|
476
|
+ cfgRetryHandler();
|
|
|
477
|
+ HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
|
|
|
478
|
+ httpClient = httpClientBuilder.setDefaultCredentialsProvider(credsProvider).setConnectionManager(connMgr).setRetryHandler(retryHandler).build();
|
|
|
479
|
+ }
|
|
|
480
|
+ return httpClient;
|
|
|
481
|
+
|
|
|
482
|
+ }
|
|
|
483
|
+ //------------------------------私有方法end----------------------------------------------
|
|
|
484
|
+
|
|
|
485
|
+
|
|
|
486
|
+}
|