|
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+package com.ruoyi.bestsign.utils;
|
|
|
2
|
+
|
|
|
3
|
+import org.apache.http.HttpEntity;
|
|
|
4
|
+import org.apache.http.client.config.RequestConfig;
|
|
|
5
|
+import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
6
|
+import org.apache.http.client.methods.HttpGet;
|
|
|
7
|
+import org.apache.http.client.methods.HttpPost;
|
|
|
8
|
+import org.apache.http.config.Registry;
|
|
|
9
|
+import org.apache.http.config.RegistryBuilder;
|
|
|
10
|
+import org.apache.http.conn.socket.ConnectionSocketFactory;
|
|
|
11
|
+import org.apache.http.conn.socket.PlainConnectionSocketFactory;
|
|
|
12
|
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
|
|
13
|
+import org.apache.http.entity.StringEntity;
|
|
|
14
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
15
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
16
|
+import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
|
|
17
|
+import org.apache.http.util.EntityUtils;
|
|
|
18
|
+
|
|
|
19
|
+import javax.net.ssl.SSLContext;
|
|
|
20
|
+import javax.net.ssl.TrustManager;
|
|
|
21
|
+import javax.net.ssl.X509TrustManager;
|
|
|
22
|
+import java.io.IOException;
|
|
|
23
|
+import java.io.InputStream;
|
|
|
24
|
+import java.io.UnsupportedEncodingException;
|
|
|
25
|
+import java.net.MalformedURLException;
|
|
|
26
|
+import java.net.URL;
|
|
|
27
|
+import java.net.URLEncoder;
|
|
|
28
|
+import java.security.cert.CertificateException;
|
|
|
29
|
+import java.security.cert.X509Certificate;
|
|
|
30
|
+import java.util.HashMap;
|
|
|
31
|
+import java.util.Map;
|
|
|
32
|
+
|
|
|
33
|
+public class HttpClientSender {
|
|
|
34
|
+
|
|
|
35
|
+ private static PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = null;
|
|
|
36
|
+ private static Map<String, CloseableHttpClient> httpClients = new HashMap<String, CloseableHttpClient>();
|
|
|
37
|
+
|
|
|
38
|
+ private static Object o = new Object();
|
|
|
39
|
+
|
|
|
40
|
+ public static String sendHttpPost(String host, String method, String urlParams, String sendData) throws IOException {
|
|
|
41
|
+
|
|
|
42
|
+ String requestUrl = host + method + urlParams;
|
|
|
43
|
+
|
|
|
44
|
+ Map<String, Object> response = request("POST", requestUrl, sendData, null);
|
|
|
45
|
+ int responseCode = Integer.parseInt(response.get("responseCode").toString());
|
|
|
46
|
+ byte[] responseBytes = (byte[]) response.get("responseData");
|
|
|
47
|
+ String responseString;
|
|
|
48
|
+ try {
|
|
|
49
|
+ responseString = new String(responseBytes, "UTF-8");
|
|
|
50
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
51
|
+ responseString = new String(responseBytes);
|
|
|
52
|
+ }
|
|
|
53
|
+ //请求返回结果无论成功失败,http-status均为200
|
|
|
54
|
+ if (responseCode == 200) {
|
|
|
55
|
+ //返回结果
|
|
|
56
|
+ return responseString;
|
|
|
57
|
+ } else {
|
|
|
58
|
+ throw new IOException(responseCode + ":" + responseString);
|
|
|
59
|
+ }
|
|
|
60
|
+ }
|
|
|
61
|
+
|
|
|
62
|
+ public static byte[] sendHttpGet(String host, String method, String urlParams) throws IOException {
|
|
|
63
|
+ String requestUrl = host + method + urlParams;
|
|
|
64
|
+ Map<String, Object> response = request("GET", requestUrl, null, null);
|
|
|
65
|
+ int responseCode = Integer.parseInt(response.get("responseCode").toString());
|
|
|
66
|
+ byte[] responseBytes = (byte[]) response.get("responseData");
|
|
|
67
|
+ //请求返回结果无论成功失败,http-status均为200
|
|
|
68
|
+ if (responseCode == 200) {
|
|
|
69
|
+ //返回结果
|
|
|
70
|
+ return responseBytes;
|
|
|
71
|
+ } else {
|
|
|
72
|
+ throw new IOException(responseCode + "");
|
|
|
73
|
+ }
|
|
|
74
|
+ }
|
|
|
75
|
+
|
|
|
76
|
+ public static String urlencode(String data) {
|
|
|
77
|
+ return urlencode(data, "UTF-8");
|
|
|
78
|
+ }
|
|
|
79
|
+
|
|
|
80
|
+ public static String urlencode(String data, String charset) {
|
|
|
81
|
+ try {
|
|
|
82
|
+ return URLEncoder.encode(data, charset);
|
|
|
83
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
84
|
+ throw new RuntimeException(e.getMessage(), e);
|
|
|
85
|
+ }
|
|
|
86
|
+ }
|
|
|
87
|
+
|
|
|
88
|
+ public static Map<String, Object> request(String method, String url, Object sendData, Map<String, String> headers) throws IOException {
|
|
|
89
|
+
|
|
|
90
|
+
|
|
|
91
|
+ String requestPath;
|
|
|
92
|
+ try {
|
|
|
93
|
+ requestPath = new URL(url).getPath();
|
|
|
94
|
+ } catch (MalformedURLException e) {
|
|
|
95
|
+ throw new RuntimeException(e.getMessage(), e);
|
|
|
96
|
+ }
|
|
|
97
|
+ CloseableHttpClient httpClient = getHttpClient(requestPath);
|
|
|
98
|
+
|
|
|
99
|
+ Map<String, Object> response = null;
|
|
|
100
|
+ if ("POST".equals(method)) {
|
|
|
101
|
+ response = sendPost(httpClient, url, headers, sendData);
|
|
|
102
|
+ } else {
|
|
|
103
|
+ response = sendGet(httpClient, url, headers);
|
|
|
104
|
+ }
|
|
|
105
|
+ return response;
|
|
|
106
|
+ }
|
|
|
107
|
+
|
|
|
108
|
+ private static Map<String, Object> sendPost(CloseableHttpClient httpClient, String url, Map<String, String> headers, Object sendData) throws IOException {
|
|
|
109
|
+ String tag = "[HttpRequester] [POST " + url + "]";
|
|
|
110
|
+ int responseCode = -1;
|
|
|
111
|
+ byte[] responseBytes = null;
|
|
|
112
|
+
|
|
|
113
|
+ HttpPost request = new HttpPost(url);
|
|
|
114
|
+ if (headers != null && headers.size() > 0) {
|
|
|
115
|
+ for (String name : headers.keySet()) {
|
|
|
116
|
+ String value = headers.get(name);
|
|
|
117
|
+ request.setHeader(name, value);
|
|
|
118
|
+ }
|
|
|
119
|
+ }
|
|
|
120
|
+ if (sendData != null) {
|
|
|
121
|
+ StringEntity stringEntity = new StringEntity((String) sendData, "UTF-8");
|
|
|
122
|
+ stringEntity.setContentType("application/json");
|
|
|
123
|
+ request.setEntity(stringEntity);
|
|
|
124
|
+
|
|
|
125
|
+ HttpEntity httpEntity = null;
|
|
|
126
|
+ IOException exception = null;
|
|
|
127
|
+ for (int i = 0; i < 3; i++) {
|
|
|
128
|
+ try {
|
|
|
129
|
+ CloseableHttpResponse response = httpClient.execute(request);
|
|
|
130
|
+ responseCode = response.getStatusLine().getStatusCode();
|
|
|
131
|
+ httpEntity = response.getEntity();
|
|
|
132
|
+
|
|
|
133
|
+ String responseBody = EntityUtils.toString(httpEntity, "utf-8");
|
|
|
134
|
+ if (responseBody != null) {
|
|
|
135
|
+ responseBytes = responseBody.getBytes();
|
|
|
136
|
+ } else {
|
|
|
137
|
+ InputStream respStream = null;
|
|
|
138
|
+ try {
|
|
|
139
|
+ respStream = httpEntity.getContent();
|
|
|
140
|
+ int respBodySize = respStream.available();
|
|
|
141
|
+ if (respBodySize <= 0)
|
|
|
142
|
+ throw new IOException("Invalid respBodySize: " + respBodySize);
|
|
|
143
|
+ responseBytes = new byte[respBodySize];
|
|
|
144
|
+ if (respStream.read(responseBytes) != respBodySize)
|
|
|
145
|
+ throw new IOException("Read respBody Error");
|
|
|
146
|
+ } catch (Exception e) {
|
|
|
147
|
+ } finally {
|
|
|
148
|
+ if (respStream != null) {
|
|
|
149
|
+ respStream.close();
|
|
|
150
|
+ }
|
|
|
151
|
+ }
|
|
|
152
|
+ }
|
|
|
153
|
+
|
|
|
154
|
+ exception = null;
|
|
|
155
|
+ break;
|
|
|
156
|
+ } catch (UnsupportedOperationException e) {
|
|
|
157
|
+ try {
|
|
|
158
|
+ EntityUtils.consume(httpEntity);
|
|
|
159
|
+ } catch (IOException e2) {
|
|
|
160
|
+
|
|
|
161
|
+ }
|
|
|
162
|
+ throw new RuntimeException(e.getMessage(), e);
|
|
|
163
|
+ } catch (IOException e) {
|
|
|
164
|
+ e.printStackTrace();
|
|
|
165
|
+ exception = e;
|
|
|
166
|
+ try {
|
|
|
167
|
+ EntityUtils.consume(httpEntity);
|
|
|
168
|
+ } catch (IOException e2) {
|
|
|
169
|
+
|
|
|
170
|
+ }
|
|
|
171
|
+ if (i < 2) {
|
|
|
172
|
+ try {
|
|
|
173
|
+ Thread.sleep(5);
|
|
|
174
|
+ } catch (InterruptedException e2) {
|
|
|
175
|
+
|
|
|
176
|
+ }
|
|
|
177
|
+ }
|
|
|
178
|
+ }
|
|
|
179
|
+ }
|
|
|
180
|
+ if (exception != null) {
|
|
|
181
|
+ throw exception;
|
|
|
182
|
+ }
|
|
|
183
|
+
|
|
|
184
|
+ }
|
|
|
185
|
+ Map<String, Object> response = new HashMap<String, Object>();
|
|
|
186
|
+ response.put("responseCode", responseCode);
|
|
|
187
|
+ response.put("responseData", responseBytes);
|
|
|
188
|
+ String loggerResponseString = getLoggerString(responseBytes, 256);
|
|
|
189
|
+ return response;
|
|
|
190
|
+ }
|
|
|
191
|
+
|
|
|
192
|
+ private static Map<String, Object> sendGet(CloseableHttpClient httpClient, String url, Map<String, String> headers) throws IOException {
|
|
|
193
|
+ String tag = "[HttpRequester] [GET " + url + "]";
|
|
|
194
|
+ int responseCode = -1;
|
|
|
195
|
+ byte[] responseBytes = null;
|
|
|
196
|
+
|
|
|
197
|
+ HttpGet request = new HttpGet(url);
|
|
|
198
|
+ if (headers != null && headers.size() > 0) {
|
|
|
199
|
+ for (String name : headers.keySet()) {
|
|
|
200
|
+ String value = headers.get(name);
|
|
|
201
|
+ request.setHeader(name, value);
|
|
|
202
|
+ }
|
|
|
203
|
+ }
|
|
|
204
|
+ HttpEntity httpEntity = null;
|
|
|
205
|
+ IOException exception = null;
|
|
|
206
|
+ for (int i = 0; i < 3; i++) {
|
|
|
207
|
+ try {
|
|
|
208
|
+ CloseableHttpResponse response = httpClient.execute(request);
|
|
|
209
|
+ responseCode = response.getStatusLine().getStatusCode();
|
|
|
210
|
+ httpEntity = response.getEntity();
|
|
|
211
|
+
|
|
|
212
|
+ byte[] responseBody = EntityUtils.toByteArray(httpEntity);
|
|
|
213
|
+ if (responseBody != null) {
|
|
|
214
|
+ responseBytes = responseBody;
|
|
|
215
|
+ } else {
|
|
|
216
|
+ InputStream respStream = null;
|
|
|
217
|
+ try {
|
|
|
218
|
+ respStream = httpEntity.getContent();
|
|
|
219
|
+ int respBodySize = respStream.available();
|
|
|
220
|
+ if (respBodySize <= 0)
|
|
|
221
|
+ throw new IOException("Invalid respBodySize: " + respBodySize);
|
|
|
222
|
+ responseBytes = new byte[respBodySize];
|
|
|
223
|
+ if (respStream.read(responseBytes) != respBodySize)
|
|
|
224
|
+ throw new IOException("Read respBody Error");
|
|
|
225
|
+ } catch (Exception e) {
|
|
|
226
|
+ } finally {
|
|
|
227
|
+ if (respStream != null) {
|
|
|
228
|
+ respStream.close();
|
|
|
229
|
+ }
|
|
|
230
|
+ }
|
|
|
231
|
+ }
|
|
|
232
|
+
|
|
|
233
|
+ exception = null;
|
|
|
234
|
+ break;
|
|
|
235
|
+ } catch (UnsupportedOperationException e) {
|
|
|
236
|
+ try {
|
|
|
237
|
+ EntityUtils.consume(httpEntity);
|
|
|
238
|
+ } catch (IOException e2) {
|
|
|
239
|
+
|
|
|
240
|
+ }
|
|
|
241
|
+ throw new RuntimeException(e.getMessage(), e);
|
|
|
242
|
+ } catch (IOException e) {
|
|
|
243
|
+ e.printStackTrace();
|
|
|
244
|
+ exception = e;
|
|
|
245
|
+ try {
|
|
|
246
|
+ EntityUtils.consume(httpEntity);
|
|
|
247
|
+ } catch (IOException e2) {
|
|
|
248
|
+
|
|
|
249
|
+ }
|
|
|
250
|
+ if (i < 2) {
|
|
|
251
|
+ try {
|
|
|
252
|
+ Thread.sleep(5);
|
|
|
253
|
+ } catch (InterruptedException e2) {
|
|
|
254
|
+
|
|
|
255
|
+ }
|
|
|
256
|
+ }
|
|
|
257
|
+ }
|
|
|
258
|
+ }
|
|
|
259
|
+ if (exception != null) {
|
|
|
260
|
+ throw exception;
|
|
|
261
|
+ }
|
|
|
262
|
+ Map<String, Object> response = new HashMap<String, Object>();
|
|
|
263
|
+ response.put("responseCode", responseCode);
|
|
|
264
|
+ response.put("responseData", responseBytes);
|
|
|
265
|
+ String loggerResponseString = getLoggerString(responseBytes, 256);
|
|
|
266
|
+ System.out.println(tag + " response " + responseCode + " " + loggerResponseString);
|
|
|
267
|
+ return response;
|
|
|
268
|
+ }
|
|
|
269
|
+
|
|
|
270
|
+ private static String getLoggerString(final byte[] data, int maxLength) {
|
|
|
271
|
+ String loggerString;
|
|
|
272
|
+ if (data.length > maxLength) {
|
|
|
273
|
+ byte[] shortData = new byte[maxLength];
|
|
|
274
|
+ System.arraycopy(data, 0, shortData, 0, shortData.length);
|
|
|
275
|
+ try {
|
|
|
276
|
+ loggerString = new String(shortData, "UTF-8") + "...";
|
|
|
277
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
278
|
+ loggerString = new String(shortData) + "...";
|
|
|
279
|
+ }
|
|
|
280
|
+ } else {
|
|
|
281
|
+ try {
|
|
|
282
|
+ loggerString = new String(data, "UTF-8");
|
|
|
283
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
284
|
+ loggerString = new String(data);
|
|
|
285
|
+ }
|
|
|
286
|
+ }
|
|
|
287
|
+
|
|
|
288
|
+ char[] chars = new char[loggerString.length()];
|
|
|
289
|
+ loggerString.getChars(0, loggerString.length(), chars, 0);
|
|
|
290
|
+ for (int i = 0; i < chars.length; i++) {
|
|
|
291
|
+ char c = chars[i];
|
|
|
292
|
+ if (c == '\n' || c == '\r') {
|
|
|
293
|
+ chars[i] = ' ';
|
|
|
294
|
+ }
|
|
|
295
|
+ }
|
|
|
296
|
+ return new String(chars);
|
|
|
297
|
+ }
|
|
|
298
|
+
|
|
|
299
|
+ private static CloseableHttpClient getHttpClient(String requestPath) {
|
|
|
300
|
+ if (httpClients.containsKey(requestPath)) {
|
|
|
301
|
+ return httpClients.get(requestPath);
|
|
|
302
|
+ }
|
|
|
303
|
+ if (poolingHttpClientConnectionManager == null) {
|
|
|
304
|
+ synchronized (o) {
|
|
|
305
|
+ if (poolingHttpClientConnectionManager == null) {
|
|
|
306
|
+ poolingHttpClientConnectionManager = HttpClientUtils.createHttpClientConnectionManager();
|
|
|
307
|
+ }
|
|
|
308
|
+ }
|
|
|
309
|
+ }
|
|
|
310
|
+ synchronized (httpClients) {
|
|
|
311
|
+ if (httpClients.containsKey(requestPath)) {
|
|
|
312
|
+ return httpClients.get(requestPath);
|
|
|
313
|
+ }
|
|
|
314
|
+ CloseableHttpClient httpClient = HttpClientUtils.createHttpClient(poolingHttpClientConnectionManager);
|
|
|
315
|
+ httpClients.put(requestPath, httpClient);
|
|
|
316
|
+ return httpClient;
|
|
|
317
|
+ }
|
|
|
318
|
+ }
|
|
|
319
|
+
|
|
|
320
|
+ private static class HttpClientUtils {
|
|
|
321
|
+
|
|
|
322
|
+ // 默认连接超时
|
|
|
323
|
+ private static int defaultConnectTimeout = 6000;
|
|
|
324
|
+ // 默认读取超时
|
|
|
325
|
+ private static int defaultReadTimeout = 30000;
|
|
|
326
|
+
|
|
|
327
|
+ public static CloseableHttpClient createHttpClient(PoolingHttpClientConnectionManager connManager) {
|
|
|
328
|
+ //HttpHost httpHost = new HttpHost("10.211.55.4", 8888);
|
|
|
329
|
+ CloseableHttpClient httpClient = HttpClients.custom()
|
|
|
330
|
+ //.setProxy(httpHost)
|
|
|
331
|
+ .setConnectionManager(connManager)
|
|
|
332
|
+ .disableContentCompression()
|
|
|
333
|
+ .setSSLContext(getSslcontext())
|
|
|
334
|
+ .setDefaultRequestConfig(getRequestConfig())
|
|
|
335
|
+ .build();
|
|
|
336
|
+ return httpClient;
|
|
|
337
|
+ }
|
|
|
338
|
+
|
|
|
339
|
+ public static PoolingHttpClientConnectionManager createHttpClientConnectionManager() {
|
|
|
340
|
+ SSLConnectionSocketFactory sslConnectionSocketFactory = null;
|
|
|
341
|
+ try {
|
|
|
342
|
+ sslConnectionSocketFactory = new SSLConnectionSocketFactory(getSslcontext(), SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
|
|
|
343
|
+ } catch (Exception e) {
|
|
|
344
|
+ throw new RuntimeException(e.getMessage(), e);
|
|
|
345
|
+ }
|
|
|
346
|
+ Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
|
|
|
347
|
+ .register("https", sslConnectionSocketFactory)
|
|
|
348
|
+ .register("http", new PlainConnectionSocketFactory())
|
|
|
349
|
+ .build();
|
|
|
350
|
+ PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
|
|
|
351
|
+ cm.setMaxTotal(500);
|
|
|
352
|
+ cm.setDefaultMaxPerRoute(500);
|
|
|
353
|
+ return cm;
|
|
|
354
|
+ }
|
|
|
355
|
+
|
|
|
356
|
+ private static RequestConfig getRequestConfig() {
|
|
|
357
|
+ RequestConfig defaultRequestConfig = RequestConfig.custom()
|
|
|
358
|
+ .setConnectionRequestTimeout(defaultConnectTimeout)
|
|
|
359
|
+ .setSocketTimeout(defaultReadTimeout)
|
|
|
360
|
+ .build();
|
|
|
361
|
+ return defaultRequestConfig;
|
|
|
362
|
+ }
|
|
|
363
|
+
|
|
|
364
|
+ private static SSLContext getSslcontext() {
|
|
|
365
|
+ SSLContext sslContext = null;
|
|
|
366
|
+ try {
|
|
|
367
|
+ sslContext = SSLContext.getInstance("TLS");
|
|
|
368
|
+ TrustManager tm = new X509TrustManager() {
|
|
|
369
|
+ @Override
|
|
|
370
|
+ public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
|
|
|
371
|
+ }
|
|
|
372
|
+
|
|
|
373
|
+ @Override
|
|
|
374
|
+ public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
|
|
|
375
|
+ }
|
|
|
376
|
+
|
|
|
377
|
+ @Override
|
|
|
378
|
+ public X509Certificate[] getAcceptedIssuers() {
|
|
|
379
|
+ return null;
|
|
|
380
|
+ }
|
|
|
381
|
+ };
|
|
|
382
|
+ sslContext.init(null, new TrustManager[]{tm}, null);
|
|
|
383
|
+
|
|
|
384
|
+ } catch (Exception e) {
|
|
|
385
|
+ e.printStackTrace();
|
|
|
386
|
+ }
|
|
|
387
|
+ return sslContext;
|
|
|
388
|
+ }
|
|
|
389
|
+ }
|
|
|
390
|
+}
|