提交 | 用户 | 时间
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             // 设置通用的请求属性
145             conn.setRequestProperty("accept", "*/*");
146             conn.setRequestProperty("connection", "Keep-Alive");
147             conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
148
149             // 发送POST请求必须设置如下两行
150             conn.setDoOutput(true);
151             conn.setDoInput(true);
152             // 获取URLConnection对象对应的输出流
153             out = new PrintWriter(conn.getOutputStream());
154             // 发送请求参数
155             out.print(json);
156             // flush输出流的缓冲
157             out.flush();
158             // 定义BufferedReader输入流来读取URL的响应
159             in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
160             String line;
161             while ((line = in.readLine()) != null) {
162                 result += line;
163             }
164         } catch (Exception e) {
165             System.out.println("发送 POST 请求出现异常!" + e);
166             e.printStackTrace();
167         }
168         //使用finally块来关闭输出流、输入流
169         finally {
170             try {
171                 if (out != null) {
172                     out.close();
173                 }
174                 if (in != null) {
175                     in.close();
176                 }
177             } catch (IOException ex) {
178                 ex.printStackTrace();
179             }
180         }
181         return result;
182     }
183
184     /**
185      * 向指定 URL 发送POST方法的请求
186      *
187      * @param url
188      * @param json
189      * @param authorization
190      * @return
191      */
192     public static String sendPost(String url, String json, String authorization) {
193         PrintWriter out = null;
194         BufferedReader in = null;
195         String result = "";
196         try {
197             URL realUrl = new URL(url);
198             // 打开和URL之间的连接
199             URLConnection conn = realUrl.openConnection();
200             // 设置通用的请求属性
201             conn.setRequestProperty("Authorization", authorization);
202             conn.setRequestProperty("accept", "*/*");
203             conn.setRequestProperty("connection", "Keep-Alive");
204             conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
205
206             // 发送POST请求必须设置如下两行
207             conn.setDoOutput(true);
208             conn.setDoInput(true);
209             // 获取URLConnection对象对应的输出流
210             out = new PrintWriter(conn.getOutputStream());
211             // 发送请求参数
212             out.print(json);
213             // flush输出流的缓冲
214             out.flush();
215             // 定义BufferedReader输入流来读取URL的响应
216             in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
217             String line;
218             while ((line = in.readLine()) != null) {
219                 result += line;
220             }
221         } catch (Exception e) {
222             System.out.println("发送 POST 请求出现异常!" + e);
223             e.printStackTrace();
224         }
225         //使用finally块来关闭输出流、输入流
226         finally {
227             try {
228                 if (out != null) {
229                     out.close();
230                 }
231                 if (in != null) {
232                     in.close();
233                 }
234             } catch (IOException ex) {
235                 ex.printStackTrace();
236             }
237         }
238         return result;
239     }
ce910c 240
H 241     /**
242      *
243      * @param url
244      * @param json
245      * @param charset
246      * @param token
247      * @return
248      */
249     public static String doPost(String url, String json, String charset, String token) {
250         org.apache.http.client.HttpClient httpClient = null;
251         HttpPost httpPost = null;
252         String result = null;
253         try {
254             httpClient = new SSLClient();
255             httpPost = new HttpPost(url);
256             //设置参数
257             httpPost.addHeader("Accept", "application/json");
258             httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");
259             httpPost.addHeader("token", token);
260             StringEntity stringEntity = new StringEntity(json);
261             stringEntity.setContentEncoding("UTF-8");
262             stringEntity.setContentType("application/json");
263             httpPost.setEntity(stringEntity);
264             HttpResponse response = httpClient.execute(httpPost);
265             if (response != null) {
266                 HttpEntity resEntity = response.getEntity();
267                 if (resEntity != null) {
268                     result = EntityUtils.toString(resEntity, charset);
269                 }
270             }
271         } catch (Exception ex) {
272             ex.printStackTrace();
273         }
274         return result;
275     }
a6de49 276 }