|
|
@@ -1,16 +1,23 @@
|
|
1
|
1
|
package midware.util.helper;
|
|
2
|
2
|
|
|
|
3
|
+import com.alibaba.fastjson2.JSON;
|
|
3
|
4
|
import lombok.extern.slf4j.Slf4j;
|
|
|
5
|
+import org.apache.http.HttpEntity;
|
|
|
6
|
+import org.apache.http.HttpStatus;
|
|
|
7
|
+import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
8
|
+import org.apache.http.client.methods.HttpGet;
|
|
|
9
|
+import org.apache.http.client.methods.HttpPost;
|
|
|
10
|
+import org.apache.http.conn.ssl.NoopHostnameVerifier;
|
|
|
11
|
+import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
|
|
|
12
|
+import org.apache.http.entity.StringEntity;
|
|
|
13
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
14
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
15
|
+import org.apache.http.ssl.SSLContextBuilder;
|
|
|
16
|
+import org.apache.http.util.EntityUtils;
|
|
4
|
17
|
import org.springframework.stereotype.Component;
|
|
5
|
18
|
|
|
6
|
|
-import javax.net.ssl.*;
|
|
7
|
|
-import java.io.*;
|
|
8
|
|
-import java.net.ConnectException;
|
|
9
|
|
-import java.net.SocketTimeoutException;
|
|
10
|
|
-import java.net.URL;
|
|
11
|
|
-import java.net.URLConnection;
|
|
|
19
|
+import javax.net.ssl.SSLContext;
|
|
12
|
20
|
import java.nio.charset.StandardCharsets;
|
|
13
|
|
-import java.security.cert.X509Certificate;
|
|
14
|
21
|
|
|
15
|
22
|
@Component
|
|
16
|
23
|
@Slf4j
|
|
|
@@ -21,238 +28,74 @@ public class HttpHelper {
|
|
21
|
28
|
* @param url 发送请求的 URL
|
|
22
|
29
|
* @return 所代表远程资源的响应结果
|
|
23
|
30
|
*/
|
|
24
|
|
- public static String sendGet(String url)
|
|
25
|
|
- {
|
|
26
|
|
- return sendGet(url, "");
|
|
27
|
|
- }
|
|
28
|
|
-
|
|
29
|
|
- /**
|
|
30
|
|
- * 向指定 URL 发送GET方法的请求
|
|
31
|
|
- *
|
|
32
|
|
- * @param url 发送请求的 URL
|
|
33
|
|
- * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
|
|
34
|
|
- * @return 所代表远程资源的响应结果
|
|
35
|
|
- */
|
|
36
|
|
- public static String sendGet(String url, String param)
|
|
37
|
|
- {
|
|
38
|
|
- return sendGet(url, param, "UTF-8");
|
|
39
|
|
- }
|
|
40
|
|
-
|
|
41
|
|
- /**
|
|
42
|
|
- * 向指定 URL 发送GET方法的请求
|
|
43
|
|
- *
|
|
44
|
|
- * @param url 发送请求的 URL
|
|
45
|
|
- * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
|
|
46
|
|
- * @param contentType 编码类型
|
|
47
|
|
- * @return 所代表远程资源的响应结果
|
|
48
|
|
- */
|
|
49
|
|
- public static String sendGet(String url, String param, String contentType)
|
|
50
|
|
- {
|
|
51
|
|
- StringBuilder result = new StringBuilder();
|
|
52
|
|
- BufferedReader in = null;
|
|
53
|
|
- try
|
|
54
|
|
- {
|
|
55
|
|
- String urlNameString = StringHelper.isEmpty(param) ? url + "?" + param : url;
|
|
56
|
|
- log.info("sendGet - {}", urlNameString);
|
|
57
|
|
- URL realUrl = new URL(urlNameString);
|
|
58
|
|
- URLConnection connection = realUrl.openConnection();
|
|
59
|
|
- connection.setRequestProperty("accept", "*/*");
|
|
60
|
|
- connection.setRequestProperty("connection", "Keep-Alive");
|
|
61
|
|
- connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
|
62
|
|
- connection.connect();
|
|
63
|
|
- in = new BufferedReader(new InputStreamReader(connection.getInputStream(), contentType));
|
|
64
|
|
- String line;
|
|
65
|
|
- while ((line = in.readLine()) != null)
|
|
66
|
|
- {
|
|
67
|
|
- result.append(line);
|
|
68
|
|
- }
|
|
69
|
|
- log.info("recv - {}", result);
|
|
70
|
|
- }
|
|
71
|
|
- catch (ConnectException e)
|
|
72
|
|
- {
|
|
73
|
|
- log.error("调用HttpUtils.sendGet ConnectException, url=" + url + ",param=" + param, e);
|
|
74
|
|
- }
|
|
75
|
|
- catch (SocketTimeoutException e)
|
|
76
|
|
- {
|
|
77
|
|
- log.error("调用HttpUtils.sendGet SocketTimeoutException, url=" + url + ",param=" + param, e);
|
|
78
|
|
- }
|
|
79
|
|
- catch (IOException e)
|
|
80
|
|
- {
|
|
81
|
|
- log.error("调用HttpUtils.sendGet IOException, url=" + url + ",param=" + param, e);
|
|
82
|
|
- }
|
|
83
|
|
- catch (Exception e)
|
|
84
|
|
- {
|
|
85
|
|
- log.error("调用HttpsUtil.sendGet Exception, url=" + url + ",param=" + param, e);
|
|
86
|
|
- }
|
|
87
|
|
- finally
|
|
88
|
|
- {
|
|
89
|
|
- try
|
|
90
|
|
- {
|
|
91
|
|
- if (in != null)
|
|
92
|
|
- {
|
|
93
|
|
- in.close();
|
|
94
|
|
- }
|
|
95
|
|
- }
|
|
96
|
|
- catch (Exception ex)
|
|
97
|
|
- {
|
|
98
|
|
- log.error("调用in.close Exception, url=" + url + ",param=" + param, ex);
|
|
|
31
|
+ public static String get(String url) {
|
|
|
32
|
+ String result = "";
|
|
|
33
|
+ try {
|
|
|
34
|
+ // 创建httpclient对象
|
|
|
35
|
+ CloseableHttpClient httpClient = HttpClients.createDefault();
|
|
|
36
|
+ // 创建请求对象
|
|
|
37
|
+ HttpGet httpGet = new HttpGet(url);
|
|
|
38
|
+ // 发送请求,接收响应
|
|
|
39
|
+ CloseableHttpResponse response = httpClient.execute(httpGet);
|
|
|
40
|
+ // 获取状态码
|
|
|
41
|
+ int statusCode = response.getStatusLine().getStatusCode();
|
|
|
42
|
+ if (statusCode == HttpStatus.SC_OK) {
|
|
|
43
|
+ // 获取响应数据
|
|
|
44
|
+ HttpEntity entity = response.getEntity();
|
|
|
45
|
+ result = EntityUtils.toString(entity,StandardCharsets.UTF_8);
|
|
99
|
46
|
}
|
|
|
47
|
+ // 关闭资源
|
|
|
48
|
+ response.close();
|
|
|
49
|
+ httpClient.close();
|
|
|
50
|
+ } catch (Exception ex) {
|
|
|
51
|
+ log.error("get请求异常:" + url, ex);
|
|
100
|
52
|
}
|
|
101
|
|
- return result.toString();
|
|
|
53
|
+ return result;
|
|
102
|
54
|
}
|
|
103
|
55
|
|
|
104
|
56
|
/**
|
|
105
|
57
|
* 向指定 URL 发送POST方法的请求
|
|
106
|
58
|
*
|
|
107
|
59
|
* @param url 发送请求的 URL
|
|
108
|
|
- * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
|
|
|
60
|
+ * @param param 请求参数json
|
|
109
|
61
|
* @return 所代表远程资源的响应结果
|
|
110
|
62
|
*/
|
|
111
|
|
- public static String sendPost(String url, String param)
|
|
112
|
|
- {
|
|
113
|
|
- PrintWriter out = null;
|
|
114
|
|
- BufferedReader in = null;
|
|
115
|
|
- StringBuilder result = new StringBuilder();
|
|
116
|
|
- try
|
|
117
|
|
- {
|
|
118
|
|
- log.info("sendPost - {}", url);
|
|
119
|
|
- URL realUrl = new URL(url);
|
|
120
|
|
- URLConnection conn = realUrl.openConnection();
|
|
121
|
|
- conn.setRequestProperty("accept", "*/*");
|
|
122
|
|
- conn.setRequestProperty("connection", "Keep-Alive");
|
|
123
|
|
- conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
|
124
|
|
- conn.setRequestProperty("Accept-Charset", "utf-8");
|
|
125
|
|
- conn.setRequestProperty("contentType", "utf-8");
|
|
126
|
|
- conn.setDoOutput(true);
|
|
127
|
|
- conn.setDoInput(true);
|
|
128
|
|
- out = new PrintWriter(conn.getOutputStream());
|
|
129
|
|
- out.print(param);
|
|
130
|
|
- out.flush();
|
|
131
|
|
- in = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
|
|
132
|
|
- String line;
|
|
133
|
|
- while ((line = in.readLine()) != null)
|
|
134
|
|
- {
|
|
135
|
|
- result.append(line);
|
|
136
|
|
- }
|
|
137
|
|
- log.info("recv - {}", result);
|
|
138
|
|
- }
|
|
139
|
|
- catch (ConnectException e)
|
|
140
|
|
- {
|
|
141
|
|
- log.error("调用HttpUtils.sendPost ConnectException, url=" + url + ",param=" + param, e);
|
|
142
|
|
- }
|
|
143
|
|
- catch (SocketTimeoutException e)
|
|
144
|
|
- {
|
|
145
|
|
- log.error("调用HttpUtils.sendPost SocketTimeoutException, url=" + url + ",param=" + param, e);
|
|
146
|
|
- }
|
|
147
|
|
- catch (IOException e)
|
|
148
|
|
- {
|
|
149
|
|
- log.error("调用HttpUtils.sendPost IOException, url=" + url + ",param=" + param, e);
|
|
150
|
|
- }
|
|
151
|
|
- catch (Exception e)
|
|
152
|
|
- {
|
|
153
|
|
- log.error("调用HttpsUtil.sendPost Exception, url=" + url + ",param=" + param, e);
|
|
154
|
|
- }
|
|
155
|
|
- finally
|
|
156
|
|
- {
|
|
157
|
|
- try
|
|
158
|
|
- {
|
|
159
|
|
- if (out != null)
|
|
160
|
|
- {
|
|
161
|
|
- out.close();
|
|
162
|
|
- }
|
|
163
|
|
- if (in != null)
|
|
164
|
|
- {
|
|
165
|
|
- in.close();
|
|
166
|
|
- }
|
|
167
|
|
- }
|
|
168
|
|
- catch (IOException ex)
|
|
169
|
|
- {
|
|
170
|
|
- log.error("调用in.close Exception, url=" + url + ",param=" + param, ex);
|
|
171
|
|
- }
|
|
172
|
|
- }
|
|
173
|
|
- return result.toString();
|
|
174
|
|
- }
|
|
|
63
|
+ public static String post(String url, String param) {
|
|
|
64
|
+ String result = "";
|
|
|
65
|
+ try {
|
|
|
66
|
+ // 创建httpclient对象
|
|
|
67
|
+ CloseableHttpClient httpClient = HttpClients.createDefault();
|
|
|
68
|
+ if(url.startsWith("https")) {
|
|
|
69
|
+ SSLContext sslContext = new SSLContextBuilder()
|
|
|
70
|
+ .loadTrustMaterial(null, new TrustSelfSignedStrategy())
|
|
|
71
|
+ .build();
|
|
175
|
72
|
|
|
176
|
|
- public static String sendSSLPost(String url, String param)
|
|
177
|
|
- {
|
|
178
|
|
- StringBuilder result = new StringBuilder();
|
|
179
|
|
- String urlNameString = url + "?" + param;
|
|
180
|
|
- try
|
|
181
|
|
- {
|
|
182
|
|
- log.info("sendSSLPost - {}", urlNameString);
|
|
183
|
|
- SSLContext sc = SSLContext.getInstance("SSL");
|
|
184
|
|
- sc.init(null, new TrustManager[] { new TrustAnyTrustManager() }, new java.security.SecureRandom());
|
|
185
|
|
- URL console = new URL(urlNameString);
|
|
186
|
|
- HttpsURLConnection conn = (HttpsURLConnection) console.openConnection();
|
|
187
|
|
- conn.setRequestProperty("accept", "*/*");
|
|
188
|
|
- conn.setRequestProperty("connection", "Keep-Alive");
|
|
189
|
|
- conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
|
190
|
|
- conn.setRequestProperty("Accept-Charset", "utf-8");
|
|
191
|
|
- conn.setRequestProperty("contentType", "utf-8");
|
|
192
|
|
- conn.setDoOutput(true);
|
|
193
|
|
- conn.setDoInput(true);
|
|
194
|
|
-
|
|
195
|
|
- conn.setSSLSocketFactory(sc.getSocketFactory());
|
|
196
|
|
- conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
|
|
197
|
|
- conn.connect();
|
|
198
|
|
- InputStream is = conn.getInputStream();
|
|
199
|
|
- BufferedReader br = new BufferedReader(new InputStreamReader(is));
|
|
200
|
|
- String ret = "";
|
|
201
|
|
- while ((ret = br.readLine()) != null)
|
|
202
|
|
- {
|
|
203
|
|
- if (ret != null && !"".equals(ret.trim()))
|
|
204
|
|
- {
|
|
205
|
|
- result.append(new String(ret.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8));
|
|
206
|
|
- }
|
|
|
73
|
+ httpClient = HttpClients.custom()
|
|
|
74
|
+ .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
|
|
|
75
|
+ .setSSLContext(sslContext)
|
|
|
76
|
+ .build();
|
|
207
|
77
|
}
|
|
208
|
|
- log.info("recv - {}", result);
|
|
209
|
|
- conn.disconnect();
|
|
210
|
|
- br.close();
|
|
211
|
|
- }
|
|
212
|
|
- catch (ConnectException e)
|
|
213
|
|
- {
|
|
214
|
|
- log.error("调用HttpUtils.sendSSLPost ConnectException, url=" + url + ",param=" + param, e);
|
|
215
|
|
- }
|
|
216
|
|
- catch (SocketTimeoutException e)
|
|
217
|
|
- {
|
|
218
|
|
- log.error("调用HttpUtils.sendSSLPost SocketTimeoutException, url=" + url + ",param=" + param, e);
|
|
219
|
|
- }
|
|
220
|
|
- catch (IOException e)
|
|
221
|
|
- {
|
|
222
|
|
- log.error("调用HttpUtils.sendSSLPost IOException, url=" + url + ",param=" + param, e);
|
|
223
|
|
- }
|
|
224
|
|
- catch (Exception e)
|
|
225
|
|
- {
|
|
226
|
|
- log.error("调用HttpsUtil.sendSSLPost Exception, url=" + url + ",param=" + param, e);
|
|
227
|
|
- }
|
|
228
|
|
- return result.toString();
|
|
229
|
|
- }
|
|
230
|
78
|
|
|
231
|
|
- private static class TrustAnyTrustManager implements X509TrustManager
|
|
232
|
|
- {
|
|
233
|
|
- @Override
|
|
234
|
|
- public void checkClientTrusted(X509Certificate[] chain, String authType)
|
|
235
|
|
- {
|
|
236
|
|
- }
|
|
|
79
|
+ // 创建请求对象
|
|
|
80
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
81
|
+ httpPost.setEntity(new StringEntity(param,StandardCharsets.UTF_8));
|
|
|
82
|
+ httpPost.setHeader("Content-Type","application/json;charset=UTF-8");
|
|
237
|
83
|
|
|
238
|
|
- @Override
|
|
239
|
|
- public void checkServerTrusted(X509Certificate[] chain, String authType)
|
|
240
|
|
- {
|
|
241
|
|
- }
|
|
242
|
|
-
|
|
243
|
|
- @Override
|
|
244
|
|
- public X509Certificate[] getAcceptedIssuers()
|
|
245
|
|
- {
|
|
246
|
|
- return new X509Certificate[] {};
|
|
247
|
|
- }
|
|
248
|
|
- }
|
|
249
|
|
-
|
|
250
|
|
- private static class TrustAnyHostnameVerifier implements HostnameVerifier
|
|
251
|
|
- {
|
|
252
|
|
- @Override
|
|
253
|
|
- public boolean verify(String hostname, SSLSession session)
|
|
254
|
|
- {
|
|
255
|
|
- return true;
|
|
|
84
|
+ // 发送请求,接收响应
|
|
|
85
|
+ CloseableHttpResponse response = httpClient.execute(httpPost);
|
|
|
86
|
+ // 获取状态码
|
|
|
87
|
+ int statusCode = response.getStatusLine().getStatusCode();
|
|
|
88
|
+ if (statusCode == HttpStatus.SC_OK) {
|
|
|
89
|
+ // 获取响应数据
|
|
|
90
|
+ HttpEntity entity1 = response.getEntity();
|
|
|
91
|
+ result = EntityUtils.toString(entity1,StandardCharsets.UTF_8);
|
|
|
92
|
+ }
|
|
|
93
|
+ // 关闭资源
|
|
|
94
|
+ response.close();
|
|
|
95
|
+ httpClient.close();
|
|
|
96
|
+ } catch (Exception ex) {
|
|
|
97
|
+ log.error("post请求异常:" + url + (StringHelper.isNotEmpty(param) ? "|" + param : ""), ex);
|
|
256
|
98
|
}
|
|
|
99
|
+ return result;
|
|
257
|
100
|
}
|
|
258
|
101
|
}
|