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