提交 | 用户 | 时间
|
a6de49
|
1 |
package com.iailab.module.data.common.utils; |
H |
2 |
|
|
3 |
import lombok.extern.slf4j.Slf4j; |
|
4 |
import org.apache.http.Header; |
|
5 |
import org.apache.http.HttpEntity; |
|
6 |
import org.apache.http.HttpResponse; |
|
7 |
import org.apache.http.client.methods.HttpGet; |
|
8 |
import org.apache.http.client.methods.HttpPost; |
|
9 |
import org.apache.http.entity.StringEntity; |
|
10 |
import org.apache.http.util.EntityUtils; |
|
11 |
import org.springframework.stereotype.Component; |
|
12 |
import org.springframework.util.CollectionUtils; |
|
13 |
|
|
14 |
import java.io.UnsupportedEncodingException; |
|
15 |
import java.net.URLEncoder; |
|
16 |
import java.util.Map; |
|
17 |
import java.util.Objects; |
|
18 |
|
|
19 |
/** |
|
20 |
* @author PanZhibao |
|
21 |
* @date 2021年06月03日 16:40 |
|
22 |
*/ |
|
23 |
@Slf4j |
|
24 |
@Component |
|
25 |
public class HttpsRequest { |
|
26 |
|
|
27 |
/** |
|
28 |
* doGet |
|
29 |
* |
|
30 |
* @param url |
|
31 |
* @param map |
|
32 |
* @param charset |
|
33 |
* @param token |
|
34 |
* @return |
|
35 |
*/ |
|
36 |
public String doGet(String url, Map<String, String> map, String charset, String token) { |
|
37 |
org.apache.http.client.HttpClient httpClient = null; |
|
38 |
HttpGet httpGet = null; |
|
39 |
String result = null; |
|
40 |
try { |
|
41 |
httpClient = new SSLClient(); |
|
42 |
StringBuilder sb = new StringBuilder(); |
|
43 |
sb.append(url); |
|
44 |
if (!CollectionUtils.isEmpty(map)) { |
|
45 |
sb.append("?"); |
|
46 |
map.forEach((k, v) -> { |
|
47 |
try { |
|
48 |
sb.append(k + "=" + URLEncoder.encode(v, charset) + "&"); |
|
49 |
} catch (UnsupportedEncodingException e) { |
|
50 |
e.printStackTrace(); |
|
51 |
} |
|
52 |
}); |
|
53 |
sb.append("t=" + System.currentTimeMillis()); |
|
54 |
} |
|
55 |
log.info("doGet,url=" + sb.toString()); |
|
56 |
httpGet = new HttpGet(sb.toString()); |
|
57 |
//设置参数 |
|
58 |
httpGet.addHeader("Accept", "application/json"); |
|
59 |
httpGet.addHeader("Content-Type", "application/json;charset=UTF-8"); |
|
60 |
httpGet.addHeader("Authorization", token); |
|
61 |
HttpResponse response = httpClient.execute(httpGet); |
|
62 |
if (response != null) { |
|
63 |
HttpEntity resEntity = response.getEntity(); |
|
64 |
if (resEntity != null) { |
|
65 |
result = EntityUtils.toString(resEntity, charset); |
|
66 |
} |
|
67 |
} |
|
68 |
} catch (Exception ex) { |
|
69 |
ex.printStackTrace(); |
|
70 |
} |
|
71 |
return result; |
|
72 |
} |
|
73 |
|
|
74 |
/** |
|
75 |
* doGet |
|
76 |
* |
|
77 |
* @param url |
|
78 |
* @param map |
|
79 |
* @param charset |
|
80 |
* @param authorization |
|
81 |
* @return |
|
82 |
*/ |
|
83 |
public String doGetKIO(String url, Map<String, String> map, String charset, String authorization) { |
|
84 |
org.apache.http.client.HttpClient httpClient = null; |
|
85 |
HttpGet httpGet = null; |
|
86 |
String result = null; |
|
87 |
try { |
|
88 |
httpClient = new SSLClient(); |
|
89 |
StringBuilder sb = new StringBuilder(); |
|
90 |
sb.append(url); |
|
91 |
if (!CollectionUtils.isEmpty(map)) { |
|
92 |
sb.append("?"); |
|
93 |
map.forEach((k, v) -> { |
|
94 |
try { |
|
95 |
sb.append(k + "=" + URLEncoder.encode(v, charset) + "&"); |
|
96 |
} catch (UnsupportedEncodingException e) { |
|
97 |
e.printStackTrace(); |
|
98 |
} |
|
99 |
}); |
|
100 |
sb.append("t=" + System.currentTimeMillis()); |
|
101 |
} |
|
102 |
log.info("doGet,url=" + sb.toString()); |
|
103 |
httpGet = new HttpGet(sb.toString()); |
|
104 |
//设置参数 |
|
105 |
httpGet.addHeader("Accept", "application/json"); |
|
106 |
httpGet.addHeader("Content-Type", "application/json;charset=UTF-8"); |
|
107 |
httpGet.addHeader("Authorization", authorization); |
|
108 |
HttpResponse response = httpClient.execute(httpGet); |
|
109 |
if (response != null) { |
|
110 |
HttpEntity resEntity = response.getEntity(); |
|
111 |
if (resEntity != null) { |
|
112 |
result = EntityUtils.toString(resEntity, charset); |
|
113 |
} |
|
114 |
} |
|
115 |
} catch (Exception ex) { |
|
116 |
ex.printStackTrace(); |
|
117 |
} |
|
118 |
return result; |
|
119 |
} |
|
120 |
|
|
121 |
/** |
|
122 |
* doPost |
|
123 |
* |
|
124 |
* @param url |
|
125 |
* @param map |
|
126 |
* @param json |
|
127 |
* @param charset |
|
128 |
* @param token |
|
129 |
* @return |
|
130 |
*/ |
|
131 |
public String doPost(String url, Map<String, String> map, String json, String charset, String token) { |
|
132 |
org.apache.http.client.HttpClient httpClient = null; |
|
133 |
HttpPost httpPost = null; |
|
134 |
String result = null; |
|
135 |
try { |
|
136 |
httpClient = new SSLClient(); |
|
137 |
StringBuilder sb = new StringBuilder(); |
|
138 |
sb.append(url); |
|
139 |
if (!CollectionUtils.isEmpty(map)) { |
|
140 |
sb.append("?"); |
|
141 |
map.forEach((k, v) -> { |
|
142 |
try { |
|
143 |
sb.append(k + "=" + URLEncoder.encode(v, charset) + "&"); |
|
144 |
} catch (UnsupportedEncodingException e) { |
|
145 |
e.printStackTrace(); |
|
146 |
} |
|
147 |
}); |
|
148 |
sb.append("t=" + System.currentTimeMillis()); |
|
149 |
} |
|
150 |
httpPost = new HttpPost(sb.toString()); |
|
151 |
//设置参数 |
|
152 |
httpPost.addHeader("Accept", "application/json"); |
|
153 |
httpPost.addHeader("Content-Type", "application/json;charset=UTF-8"); |
|
154 |
httpPost.addHeader("token", token); |
|
155 |
StringEntity stringEntity = new StringEntity(json); |
|
156 |
stringEntity.setContentEncoding("UTF-8"); |
|
157 |
stringEntity.setContentType("application/json"); |
|
158 |
httpPost.setEntity(stringEntity); |
|
159 |
HttpResponse response = httpClient.execute(httpPost); |
|
160 |
if (response != null) { |
|
161 |
HttpEntity resEntity = response.getEntity(); |
|
162 |
if (resEntity != null) { |
|
163 |
result = EntityUtils.toString(resEntity, charset); |
|
164 |
} |
|
165 |
} |
|
166 |
} catch (Exception ex) { |
|
167 |
// ex.printStackTrace(); |
|
168 |
log.info("doPost失败,url=" + url); |
|
169 |
} |
|
170 |
return result; |
|
171 |
} |
|
172 |
|
|
173 |
|
|
174 |
/** |
|
175 |
* doPost |
|
176 |
* |
|
177 |
* @param url |
|
178 |
* @param map |
|
179 |
* @param json |
|
180 |
* @param charset |
|
181 |
* @param authorization |
|
182 |
* @return |
|
183 |
*/ |
|
184 |
public String doPostKIO(String url, Map<String, String> map, String json, String charset, String authorization) { |
|
185 |
org.apache.http.client.HttpClient httpClient = null; |
|
186 |
HttpPost httpPost = null; |
|
187 |
String result = null; |
|
188 |
try { |
|
189 |
httpClient = new SSLClient(); |
|
190 |
StringBuilder sb = new StringBuilder(); |
|
191 |
sb.append(url); |
|
192 |
if (!CollectionUtils.isEmpty(map)) { |
|
193 |
sb.append("?"); |
|
194 |
map.forEach((k, v) -> { |
|
195 |
try { |
|
196 |
sb.append(k + "=" + URLEncoder.encode(v, charset) + "&"); |
|
197 |
} catch (UnsupportedEncodingException e) { |
|
198 |
e.printStackTrace(); |
|
199 |
} |
|
200 |
}); |
|
201 |
sb.append("t=" + System.currentTimeMillis()); |
|
202 |
} |
|
203 |
httpPost = new HttpPost(sb.toString()); |
|
204 |
//设置参数 |
|
205 |
httpPost.addHeader("Accept", "application/json"); |
|
206 |
httpPost.addHeader("Content-Type", "application/json;charset=UTF-8"); |
|
207 |
httpPost.addHeader("Authorization", authorization); |
|
208 |
StringEntity stringEntity = new StringEntity(json); |
|
209 |
stringEntity.setContentEncoding("UTF-8"); |
|
210 |
stringEntity.setContentType("application/json"); |
|
211 |
httpPost.setEntity(stringEntity); |
|
212 |
HttpResponse response = httpClient.execute(httpPost); |
|
213 |
if (response != null) { |
|
214 |
HttpEntity resEntity = response.getEntity(); |
|
215 |
if (resEntity != null) { |
|
216 |
result = EntityUtils.toString(resEntity, charset); |
|
217 |
} |
|
218 |
} |
|
219 |
} catch (Exception ex) { |
|
220 |
ex.printStackTrace(); |
|
221 |
} |
|
222 |
return result; |
|
223 |
} |
|
224 |
|
|
225 |
/** |
|
226 |
* doPost |
|
227 |
* |
|
228 |
* @param url |
|
229 |
* @param map |
|
230 |
* @param json |
|
231 |
* @param charset |
|
232 |
* @param token |
|
233 |
* @return |
|
234 |
*/ |
|
235 |
public String doPostAuthorization(String url, Map<String, String> map, String json, String charset, String token) { |
|
236 |
org.apache.http.client.HttpClient httpClient = null; |
|
237 |
HttpPost httpPost = null; |
|
238 |
String result = null; |
|
239 |
try { |
|
240 |
httpClient = new SSLClient(); |
|
241 |
StringBuilder sb = new StringBuilder(); |
|
242 |
sb.append(url); |
|
243 |
if (!CollectionUtils.isEmpty(map)) { |
|
244 |
sb.append("?"); |
|
245 |
map.forEach((k, v) -> { |
|
246 |
try { |
|
247 |
sb.append(k + "=" + URLEncoder.encode(v, charset) + "&"); |
|
248 |
} catch (UnsupportedEncodingException e) { |
|
249 |
e.printStackTrace(); |
|
250 |
} |
|
251 |
}); |
|
252 |
sb.append("t=" + System.currentTimeMillis()); |
|
253 |
} |
|
254 |
httpPost = new HttpPost(sb.toString()); |
|
255 |
//设置参数 |
|
256 |
httpPost.addHeader("Accept", "application/json"); |
|
257 |
httpPost.addHeader("Content-Type", "application/json;charset=UTF-8"); |
|
258 |
httpPost.addHeader("Authorization", "Bearer{" + token + "}"); |
|
259 |
StringEntity stringEntity = new StringEntity(json); |
|
260 |
stringEntity.setContentEncoding("UTF-8"); |
|
261 |
stringEntity.setContentType("application/json"); |
|
262 |
httpPost.setEntity(stringEntity); |
|
263 |
HttpResponse response = httpClient.execute(httpPost); |
|
264 |
if (response != null) { |
|
265 |
HttpEntity resEntity = response.getEntity(); |
|
266 |
if (resEntity != null) { |
|
267 |
result = EntityUtils.toString(resEntity, charset); |
|
268 |
} |
|
269 |
} |
|
270 |
} catch (Exception ex) { |
|
271 |
ex.printStackTrace(); |
|
272 |
} |
|
273 |
return result; |
|
274 |
} |
|
275 |
|
|
276 |
/** |
|
277 |
* doPost |
|
278 |
* |
|
279 |
* @param url |
|
280 |
* @param map |
|
281 |
* @param json |
|
282 |
* @param charset |
|
283 |
* @return |
|
284 |
*/ |
|
285 |
public String doPostToken(String url, Map<String, String> map, String json, String charset) { |
|
286 |
org.apache.http.client.HttpClient httpClient = null; |
|
287 |
HttpPost httpPost = null; |
|
288 |
String result = null; |
|
289 |
try { |
|
290 |
httpClient = new SSLClient(); |
|
291 |
StringBuilder sb = new StringBuilder(); |
|
292 |
sb.append(url); |
|
293 |
if (!CollectionUtils.isEmpty(map)) { |
|
294 |
sb.append("?"); |
|
295 |
map.forEach((k, v) -> { |
|
296 |
try { |
|
297 |
sb.append(k + "=" + URLEncoder.encode(v, charset) + "&"); |
|
298 |
} catch (UnsupportedEncodingException e) { |
|
299 |
e.printStackTrace(); |
|
300 |
} |
|
301 |
}); |
|
302 |
sb.append("t=" + System.currentTimeMillis()); |
|
303 |
} |
|
304 |
httpPost = new HttpPost(sb.toString()); |
|
305 |
//设置参数 |
|
306 |
httpPost.addHeader("Accept", "application/json"); |
|
307 |
httpPost.addHeader("Content-Type", "application/json;charset=UTF-8"); |
|
308 |
StringEntity stringEntity = new StringEntity(json); |
|
309 |
stringEntity.setContentEncoding("UTF-8"); |
|
310 |
stringEntity.setContentType("application/json"); |
|
311 |
httpPost.setEntity(stringEntity); |
|
312 |
HttpResponse response = httpClient.execute(httpPost); |
|
313 |
if (response != null) { |
|
314 |
Header[] resHeader = response.getHeaders("X-Auth-Tkn"); |
|
315 |
if (resHeader != null) { |
|
316 |
result = resHeader[0].getValue(); |
|
317 |
} |
|
318 |
} |
|
319 |
} catch (Exception ex) { |
|
320 |
ex.printStackTrace(); |
|
321 |
} |
|
322 |
return result; |
|
323 |
} |
|
324 |
|
|
325 |
/** |
|
326 |
* doGet |
|
327 |
* |
|
328 |
* @param url |
|
329 |
* @param map |
|
330 |
* @param charset |
|
331 |
* @param token |
|
332 |
* @param tMap |
|
333 |
* @return |
|
334 |
*/ |
|
335 |
public String doGetSDData(String url, Map<String, String> map, String charset, String token, Map<String, String> tMap) { |
|
336 |
org.apache.http.client.HttpClient httpClient = null; |
|
337 |
HttpGet httpGet = null; |
|
338 |
String result = null; |
|
339 |
try { |
|
340 |
httpClient = new SSLClient(); |
|
341 |
StringBuilder sb = new StringBuilder(); |
|
342 |
sb.append(url); |
|
343 |
if (!CollectionUtils.isEmpty(map)) { |
|
344 |
sb.append("?"); |
|
345 |
map.forEach((k, v) -> { |
|
346 |
try { |
|
347 |
sb.append(k + "=" + URLEncoder.encode(v, charset) + "&"); |
|
348 |
} catch (UnsupportedEncodingException e) { |
|
349 |
e.printStackTrace(); |
|
350 |
} |
|
351 |
}); |
|
352 |
sb.append("t=" + System.currentTimeMillis()); |
|
353 |
} |
|
354 |
log.info("doGet,url=" + sb.toString()); |
|
355 |
httpGet = new HttpGet(sb.toString()); |
|
356 |
//设置参数 |
|
357 |
httpGet.addHeader("Content-Type", "application/json"); |
|
358 |
httpGet.addHeader("X-Auth-Tkn", token); |
|
359 |
if(!Objects.isNull(tMap.get("X-Forwarded-OrgSet"))){ |
|
360 |
httpGet.addHeader("X-Forwarded-OrgSet", tMap.get("X-Forwarded-OrgSet")); |
|
361 |
} |
|
362 |
if(!Objects.isNull(tMap.get("X-Forwarded-PrId"))){ |
|
363 |
httpGet.addHeader("X-Forwarded-PrId", tMap.get("X-Forwarded-PrId")); |
|
364 |
} |
|
365 |
if(!Objects.isNull(tMap.get("X-Request-Id"))){ |
|
366 |
httpGet.addHeader("X-Request-Id", tMap.get("X-Request-Id")); |
|
367 |
} |
|
368 |
HttpResponse response = httpClient.execute(httpGet); |
|
369 |
if (response != null) { |
|
370 |
HttpEntity resEntity = response.getEntity(); |
|
371 |
if (resEntity != null) { |
|
372 |
result = EntityUtils.toString(resEntity, charset); |
|
373 |
} |
|
374 |
} |
|
375 |
} catch (Exception ex) { |
|
376 |
ex.printStackTrace(); |
|
377 |
} |
|
378 |
return result; |
|
379 |
} |
|
380 |
|
|
381 |
/** |
|
382 |
* doGet |
|
383 |
* |
|
384 |
* @param url |
|
385 |
* @param map |
|
386 |
* @param charset |
|
387 |
* @param token |
|
388 |
* @return |
|
389 |
*/ |
|
390 |
public String doGetDeviceList(String url, Map<String, String> map, String charset, String token) { |
|
391 |
org.apache.http.client.HttpClient httpClient = null; |
|
392 |
HttpGet httpGet = null; |
|
393 |
String result = null; |
|
394 |
try { |
|
395 |
httpClient = new SSLClient(); |
|
396 |
StringBuilder sb = new StringBuilder(); |
|
397 |
sb.append(url); |
|
398 |
if (!CollectionUtils.isEmpty(map)) { |
|
399 |
sb.append("?"); |
|
400 |
map.forEach((k, v) -> { |
|
401 |
try { |
|
402 |
sb.append(k + "=" + URLEncoder.encode(v, charset) + "&"); |
|
403 |
} catch (UnsupportedEncodingException e) { |
|
404 |
e.printStackTrace(); |
|
405 |
} |
|
406 |
}); |
|
407 |
sb.append("t=" + System.currentTimeMillis()); |
|
408 |
} |
|
409 |
log.info("doGet,url=" + sb.toString()); |
|
410 |
httpGet = new HttpGet(sb.toString()); |
|
411 |
//设置参数 |
|
412 |
httpGet.addHeader("Content-Type", "application/json"); |
|
413 |
httpGet.addHeader("X-App-Secret", token); |
|
414 |
HttpResponse response = httpClient.execute(httpGet); |
|
415 |
if (response != null) { |
|
416 |
HttpEntity resEntity = response.getEntity(); |
|
417 |
if (resEntity != null) { |
|
418 |
result = EntityUtils.toString(resEntity, charset); |
|
419 |
} |
|
420 |
} |
|
421 |
} catch (Exception ex) { |
|
422 |
ex.printStackTrace(); |
|
423 |
} |
|
424 |
return result; |
|
425 |
} |
|
426 |
} |