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