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