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