1 package com.mshc.util;
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 import net.sf.json.JSONObject;
12
13 /**
14 * @author svs:
15 * @version 创建时间:2017-9-23 下午05:09:55
16 * 类说明
17 */
18 public class HttpRequestUrl {
19
20 /**
21 * 向指定URL发送GET方法的请求
22 *
23 * @param url
24 * 发送请求的URL
25 * @param param
26 * 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
27 * @return URL 所代表远程资源的响应结果
28 */
29 public static String sendGet(String url, String param) {
30 String result = "";
31 BufferedReader in = null;
32 try {
33 String urlNameString = url + "?" + param;
34 URL realUrl = new URL(urlNameString);
35 // 打开和URL之间的连接
36 URLConnection connection = realUrl.openConnection();
37 // 设置通用的请求属性
38 connection.setRequestProperty("accept", "*/*");
39 connection.setRequestProperty("connection", "Keep-Alive");
40 connection.setRequestProperty("user-agent",
41 "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
42 // 建立实际的连接
43 connection.connect();
44 // 获取所有响应头字段
45 Map<String, List<String>> map = connection.getHeaderFields();
46 // 遍历所有的响应头字段
47 for (String key : map.keySet()) {
48 System.out.println(key + "--->" + map.get(key));
49 }
50 // 定义 BufferedReader输入流来读取URL的响应
51 in = new BufferedReader(new InputStreamReader(
52 connection.getInputStream()));
53 String line;
54 while ((line = in.readLine()) != null) {
55 result += line;
56 }
57 } catch (Exception e) {
58 System.out.println("发送GET请求出现异常!" + e);
59 e.printStackTrace();
60 }
61 // 使用finally块来关闭输入流
62 finally {
63 try {
64 if (in != null) {
65 in.close();
66 }
67 } catch (Exception e2) {
68 e2.printStackTrace();
69 }
70 }
71 return result;
72 }
73
74 /**
75 * 向指定 URL 发送POST方法的请求
76 *
77 * @param url
78 * 发送请求的 URL
79 * @param param
80 * 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
81 * @return 所代表远程资源的响应结果
82 */
83 public static String sendPost(String url, String param) {
84 PrintWriter out = null;
85 BufferedReader in = null;
86 String result = "";
87 try {
88 URL realUrl = new URL(url);
89 // 打开和URL之间的连接
90 URLConnection conn = realUrl.openConnection();
91 // 设置通用的请求属性
92 conn.setRequestProperty("accept", "*/*");
93 conn.setRequestProperty("connection", "Keep-Alive");
94 conn.setRequestProperty("user-agent",
95 "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
96 // 发送POST请求必须设置如下两行
97 conn.setDoOutput(true);
98 conn.setDoInput(true);
99 // 获取URLConnection对象对应的输出流
100 out = new PrintWriter(conn.getOutputStream());
101 // 发送请求参数
102 out.print(param);
103 // flush输出流的缓冲
104 out.flush();
105 // 定义BufferedReader输入流来读取URL的响应
106 in = new BufferedReader(
107 new InputStreamReader(conn.getInputStream()));
108 String line;
109 while ((line = in.readLine()) != null) {
110 result += line;
111 }
112 } catch (Exception e) {
113 System.out.println("发送 POST 请求出现异常!"+e);
114 e.printStackTrace();
115 }
116 //使用finally块来关闭输出流、输入流
117 finally{
118 try{
119 if(out!=null){
120 out.close();
121 }
122 if(in!=null){
123 in.close();
124 }
125 }
126 catch(IOException ex){
127 ex.printStackTrace();
128 }
129 }
130 return result;
131 }
132
133 public static void main(String[] args) throws Exception {
134 //发送 GET 请求
135 //String s=HttpRequestUrl.sendGet("http://localhost:8080/Home/RequestString", "key=123&v=456");
136 // System.out.println(s);
137
138
141 String sr=HttpRequestUrl.sendPost("http://192.168.1.99:8080/MshcShopGuanjia/Zonghe_findbyfuwumoney.action", "id=82&money=50");
142 System.out.println("mmmm:"+sr);
143 145 //将Json字符串转为java对象
146 JSONObject obj = JSONObject.fromObject(sr);
147 //获取Object中的UserName
148 if (obj.has("fuwumoney")) {
149 System.out.println("fuwumoney:" + obj.getString("fuwumoney"));
150 }
151
152 }
153
154 }