提交 | 用户 | 时间
|
a6de49
|
1 |
package com.iailab.common.utils; |
H |
2 |
|
|
3 |
import java.io.BufferedReader; |
|
4 |
import java.io.IOException; |
|
5 |
import java.io.InputStreamReader; |
|
6 |
import java.io.PrintWriter; |
|
7 |
import java.net.URL; |
|
8 |
import java.net.URLConnection; |
|
9 |
import java.util.List; |
|
10 |
import java.util.Map; |
|
11 |
|
|
12 |
/** |
|
13 |
* @author PanZhibao |
|
14 |
* @date 2021年06月03日 15:31 |
|
15 |
*/ |
|
16 |
public class HttpRequest { |
|
17 |
/** |
|
18 |
* 向指定URL发送GET方法的请求 |
|
19 |
* |
|
20 |
* @param url 发送请求的URL |
|
21 |
* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。 |
|
22 |
* @return URL 所代表远程资源的响应结果 |
|
23 |
*/ |
|
24 |
public static String sendGet(String url, String param) { |
|
25 |
String result = ""; |
|
26 |
BufferedReader in = null; |
|
27 |
try { |
|
28 |
String urlNameString = url + "?" + param; |
|
29 |
URL realUrl = new URL(urlNameString); |
|
30 |
// 打开和URL之间的连接 |
|
31 |
URLConnection connection = realUrl.openConnection(); |
|
32 |
// 设置通用的请求属性 |
|
33 |
connection.setRequestProperty("accept", "*/*"); |
|
34 |
connection.setRequestProperty("connection", "Keep-Alive"); |
|
35 |
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); |
|
36 |
|
|
37 |
// 建立实际的连接 |
|
38 |
connection.connect(); |
|
39 |
// 获取所有响应头字段 |
|
40 |
Map<String, List<String>> map = connection.getHeaderFields(); |
|
41 |
// 遍历所有的响应头字段 |
|
42 |
for (String key : map.keySet()) { |
|
43 |
System.out.println(key + "--->" + map.get(key)); |
|
44 |
} |
|
45 |
// 定义 BufferedReader输入流来读取URL的响应 |
|
46 |
in = new BufferedReader(new InputStreamReader(connection.getInputStream())); |
|
47 |
String line; |
|
48 |
while ((line = in.readLine()) != null) { |
|
49 |
result += line; |
|
50 |
} |
|
51 |
} catch (Exception e) { |
|
52 |
System.out.println("发送GET请求出现异常!" + e); |
|
53 |
e.printStackTrace(); |
|
54 |
} |
|
55 |
// 使用finally块来关闭输入流 |
|
56 |
finally { |
|
57 |
try { |
|
58 |
if (in != null) { |
|
59 |
in.close(); |
|
60 |
} |
|
61 |
} catch (Exception e2) { |
|
62 |
e2.printStackTrace(); |
|
63 |
} |
|
64 |
} |
|
65 |
return result; |
|
66 |
} |
|
67 |
|
|
68 |
/** |
|
69 |
* 向指定 URL 发送POST方法的请求 |
|
70 |
* |
|
71 |
* @param url 发送请求的 URL |
|
72 |
* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。 |
|
73 |
* @return 所代表远程资源的响应结果 |
|
74 |
*/ |
|
75 |
public static String sendPost(String url, String param) { |
|
76 |
PrintWriter out = null; |
|
77 |
BufferedReader in = null; |
|
78 |
String result = ""; |
|
79 |
try { |
|
80 |
URL realUrl = new URL(url); |
|
81 |
// 打开和URL之间的连接 |
|
82 |
URLConnection conn = realUrl.openConnection(); |
|
83 |
// 设置通用的请求属性 |
|
84 |
conn.setRequestProperty("accept", "*/*"); |
|
85 |
conn.setRequestProperty("connection", "Keep-Alive"); |
|
86 |
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); |
|
87 |
|
|
88 |
// 发送POST请求必须设置如下两行 |
|
89 |
conn.setDoOutput(true); |
|
90 |
conn.setDoInput(true); |
|
91 |
// 获取URLConnection对象对应的输出流 |
|
92 |
out = new PrintWriter(conn.getOutputStream()); |
|
93 |
// 发送请求参数 |
|
94 |
out.print(param); |
|
95 |
// flush输出流的缓冲 |
|
96 |
out.flush(); |
|
97 |
// 定义BufferedReader输入流来读取URL的响应 |
|
98 |
in = new BufferedReader(new InputStreamReader(conn.getInputStream())); |
|
99 |
String line; |
|
100 |
while ((line = in.readLine()) != null) { |
|
101 |
result += line; |
|
102 |
} |
|
103 |
} catch (Exception e) { |
|
104 |
System.out.println("发送 POST 请求出现异常!" + e); |
|
105 |
e.printStackTrace(); |
|
106 |
} |
|
107 |
//使用finally块来关闭输出流、输入流 |
|
108 |
finally { |
|
109 |
try { |
|
110 |
if (out != null) { |
|
111 |
out.close(); |
|
112 |
} |
|
113 |
if (in != null) { |
|
114 |
in.close(); |
|
115 |
} |
|
116 |
} catch (IOException ex) { |
|
117 |
ex.printStackTrace(); |
|
118 |
} |
|
119 |
} |
|
120 |
return result; |
|
121 |
} |
|
122 |
} |