Jay
2024-11-08 02722a3f9eca857ce7fffea352e9f7ee692a1b71
提交 | 用户 | 时间
a6de49 1 package com.iailab.module.data.common.utils;
H 2
149dd0 3 import com.iailab.framework.security.core.util.SecurityFrameworkUtils;
H 4 import com.iailab.framework.tenant.core.context.TenantContextHolder;
ce910c 5 import org.apache.http.HttpEntity;
H 6 import org.apache.http.HttpResponse;
7 import org.apache.http.client.methods.HttpPost;
8 import org.apache.http.entity.StringEntity;
9 import org.apache.http.util.EntityUtils;
a6de49 10 import org.springframework.util.CollectionUtils;
H 11
12 import java.io.BufferedReader;
13 import java.io.IOException;
14 import java.io.InputStreamReader;
15 import java.io.PrintWriter;
16 import java.net.URL;
17 import java.net.URLConnection;
18 import java.util.List;
19 import java.util.Map;
20
21 /**
22  * @author PanZhibao
23  * @date 2021年06月03日 15:31
24  */
25 public class HttpRequest {
26     /**
27      * 向指定URL发送GET方法的请求
28      *
29      * @param url   发送请求的URL
30      * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
31      * @return URL 所代表远程资源的响应结果
32      */
33     public static String sendGet(String url, String param) {
34         String result = "";
35         BufferedReader in = null;
36         try {
37             String urlNameString = url + "?" + param;
38             URL realUrl = new URL(urlNameString);
39             // 打开和URL之间的连接
40             URLConnection connection = realUrl.openConnection();
41             // 设置通用的请求属性
42             connection.setRequestProperty("accept", "*/*");
43             connection.setRequestProperty("connection", "Keep-Alive");
44             connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
45
46             // 建立实际的连接
47             connection.connect();
48             // 获取所有响应头字段
49             Map<String, List<String>> map = connection.getHeaderFields();
50             // 遍历所有的响应头字段
51             for (String key : map.keySet()) {
52                 System.out.println(key + "--->" + map.get(key));
53             }
54             // 定义 BufferedReader输入流来读取URL的响应
55             in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
56             String line;
57             while ((line = in.readLine()) != null) {
58                 result += line;
59             }
60         } catch (Exception e) {
61             System.out.println("发送GET请求出现异常!" + e);
62             e.printStackTrace();
63         }
64         // 使用finally块来关闭输入流
65         finally {
66             try {
67                 if (in != null) {
68                     in.close();
69                 }
70             } catch (Exception e2) {
71                 e2.printStackTrace();
72             }
73         }
74         return result;
75     }
76
77     public static String sendGet(String url, Map<String, String> params, String authorization) {
78         String result = "";
79         BufferedReader in = null;
80         try {
81             StringBuilder sb = new StringBuilder();
82             sb.append(url);
83             if (!CollectionUtils.isEmpty(params)) {
84                 sb.append("?");
85                 params.forEach((k, v) -> {
86                     sb.append(k + "=" + v + "&");
87                 });
88                 sb.append("t=" + System.currentTimeMillis());
89             }
90             String urlNameString = sb.toString();
91             URL realUrl = new URL(urlNameString);
92             // 打开和URL之间的连接
93             URLConnection connection = realUrl.openConnection();
94             // 设置通用的请求属性
95             connection.setRequestProperty("Authorization", authorization);
96             connection.setRequestProperty("accept", "*/*");
97             connection.setRequestProperty("connection", "Keep-Alive");
98             connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
99
100             // 建立实际的连接
101             connection.connect();
102             // 获取所有响应头字段
103             Map<String, List<String>> map = connection.getHeaderFields();
104             // 遍历所有的响应头字段
105             for (String key : map.keySet()) {
106                 System.out.println(key + "--->" + map.get(key));
107             }
108             // 定义 BufferedReader输入流来读取URL的响应
109             in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
110             String line;
111             while ((line = in.readLine()) != null) {
112                 result += line;
113             }
114         } catch (Exception e) {
115             System.out.println("发送GET请求出现异常!" + e);
116             e.printStackTrace();
117         }
118         // 使用finally块来关闭输入流
119         finally {
120             try {
121                 if (in != null) {
122                     in.close();
123                 }
124             } catch (Exception e2) {
125                 e2.printStackTrace();
126             }
127         }
128         return result;
129     }
130
131     /**
132      * 向指定 URL 发送POST方法的请求
133      *
134      * @param url
135      * @param json
136      * @return
137      */
138     public static String sendPost(String url, String json) {
139         PrintWriter out = null;
140         BufferedReader in = null;
141         String result = "";
142         try {
143             URL realUrl = new URL(url);
144             // 打开和URL之间的连接
145             URLConnection conn = realUrl.openConnection();
146             // 设置通用的请求属性
d41f14 147             conn.setRequestProperty("content-type", "application/json");
a6de49 148             conn.setRequestProperty("accept", "*/*");
H 149             conn.setRequestProperty("connection", "Keep-Alive");
150             conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
151
152             // 发送POST请求必须设置如下两行
153             conn.setDoOutput(true);
154             conn.setDoInput(true);
155             // 获取URLConnection对象对应的输出流
156             out = new PrintWriter(conn.getOutputStream());
157             // 发送请求参数
158             out.print(json);
159             // flush输出流的缓冲
160             out.flush();
161             // 定义BufferedReader输入流来读取URL的响应
162             in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
163             String line;
164             while ((line = in.readLine()) != null) {
165                 result += line;
166             }
167         } catch (Exception e) {
168             System.out.println("发送 POST 请求出现异常!" + e);
169             e.printStackTrace();
170         }
171         //使用finally块来关闭输出流、输入流
172         finally {
173             try {
174                 if (out != null) {
175                     out.close();
176                 }
177                 if (in != null) {
178                     in.close();
179                 }
180             } catch (IOException ex) {
181                 ex.printStackTrace();
182             }
183         }
184         return result;
185     }
186
187     /**
188      * 向指定 URL 发送POST方法的请求
189      *
190      * @param url
191      * @param json
192      * @param authorization
193      * @return
194      */
195     public static String sendPost(String url, String json, String authorization) {
196         PrintWriter out = null;
197         BufferedReader in = null;
198         String result = "";
199         try {
200             URL realUrl = new URL(url);
201             // 打开和URL之间的连接
202             URLConnection conn = realUrl.openConnection();
203             // 设置通用的请求属性
204             conn.setRequestProperty("Authorization", authorization);
205             conn.setRequestProperty("accept", "*/*");
206             conn.setRequestProperty("connection", "Keep-Alive");
207             conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
208
209             // 发送POST请求必须设置如下两行
210             conn.setDoOutput(true);
211             conn.setDoInput(true);
212             // 获取URLConnection对象对应的输出流
213             out = new PrintWriter(conn.getOutputStream());
214             // 发送请求参数
215             out.print(json);
216             // flush输出流的缓冲
217             out.flush();
218             // 定义BufferedReader输入流来读取URL的响应
219             in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
220             String line;
221             while ((line = in.readLine()) != null) {
222                 result += line;
223             }
224         } catch (Exception e) {
225             System.out.println("发送 POST 请求出现异常!" + e);
226             e.printStackTrace();
227         }
228         //使用finally块来关闭输出流、输入流
229         finally {
230             try {
231                 if (out != null) {
232                     out.close();
233                 }
234                 if (in != null) {
235                     in.close();
236                 }
237             } catch (IOException ex) {
238                 ex.printStackTrace();
239             }
240         }
241         return result;
242     }
ce910c 243
H 244     /**
245      *
246      * @param url
247      * @param json
248      * @param charset
249      * @param token
250      * @return
251      */
252     public static String doPost(String url, String json, String charset, String token) {
253         org.apache.http.client.HttpClient httpClient = null;
254         HttpPost httpPost = null;
255         String result = null;
256         try {
257             httpClient = new SSLClient();
258             httpPost = new HttpPost(url);
259             //设置参数
260             httpPost.addHeader("Accept", "application/json");
261             httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");
262             httpPost.addHeader("token", token);
149dd0 263             //传递租户id
H 264             httpPost.addHeader("Tenant-Id", String.valueOf(TenantContextHolder.getTenantId()));
ce910c 265             StringEntity stringEntity = new StringEntity(json);
H 266             stringEntity.setContentEncoding("UTF-8");
267             stringEntity.setContentType("application/json");
268             httpPost.setEntity(stringEntity);
269             HttpResponse response = httpClient.execute(httpPost);
270             if (response != null) {
271                 HttpEntity resEntity = response.getEntity();
272                 if (resEntity != null) {
273                     result = EntityUtils.toString(resEntity, charset);
274                 }
275             }
276         } catch (Exception ex) {
277             ex.printStackTrace();
278         }
279         return result;
280     }
a6de49 281 }