码迷,mamicode.com
首页 > 编程语言 > 详细

Java中发送http的get、post请求

时间:2017-10-09 17:48:05      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:host   输出   关闭   name   stp   trace   tin   set   eth   

1.Post请求

 1 /**
 2      * 向指定 URL 发送POST方法的请求
 3      * 
 4      * @param url
 5      *            发送请求的 URL
 6      * @param param
 7      *            请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
 8      * @return 所代表远程资源的响应结果
 9      */
10     public static String post(String url, String param) {
11         PrintWriter out = null;
12         BufferedReader in = null;
13         String result = "";
14         try {
15             URL realUrl = new URL(url);
16             // 打开和URL之间的连接
17             URLConnection conn = realUrl.openConnection();
18             // 设置通用的请求属性
19             conn.setRequestProperty("accept", "*/*");
20             conn.setRequestProperty("connection", "Keep-Alive");
21             conn.setRequestProperty("user-agent",
22                     "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
23             // 发送POST请求必须设置如下两行
24             conn.setDoOutput(true);
25             conn.setDoInput(true);
26             // 获取URLConnection对象对应的输出流
27             out = new PrintWriter(conn.getOutputStream());
28             // 发送请求参数
29             out.print(param);
30             // flush输出流的缓冲
31             out.flush();
32             // 定义BufferedReader输入流来读取URL的响应
33             in = new BufferedReader(
34                     new InputStreamReader(conn.getInputStream()));
35             String line;
36             while ((line = in.readLine()) != null) {
37                 result += line;
38             }
39         } catch (Exception e) {
40             System.out.println("发送 POST 请求出现异常!"+e);
41             e.printStackTrace();
42         }
43         //使用finally块来关闭输出流、输入流
44         finally{
45             try{
46                 if(out!=null){
47                     out.close();
48                 }
49                 if(in!=null){
50                     in.close();
51                 }
52             }
53             catch(IOException ex){
54                 ex.printStackTrace();
55             }
56         }
57         return result;
58     }    

2.Get请求

 1  /**
 2      * 向指定URL发送GET方法的请求
 3      * 
 4      * @param url
 5      *            发送请求的URL
 6      * @param param
 7      *            请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
 8      * @return URL 所代表远程资源的响应结果
 9      */
10     public static String get(String url, String param) {
11         String result = "";
12         BufferedReader in = null;
13         try {
14             String urlNameString = url + "?" + param;
15             URL realUrl = new URL(urlNameString);
16             // 打开和URL之间的连接
17             URLConnection connection = realUrl.openConnection();
18             // 设置通用的请求属性
19             connection.setRequestProperty("accept", "*/*");
20             connection.setRequestProperty("connection", "Keep-Alive");
21             connection.setRequestProperty("user-agent",
22                     "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
23             // 建立实际的连接
24             connection.connect();
25             // 获取所有响应头字段
26             Map<String, List<String>> map = connection.getHeaderFields();
27             // 遍历所有的响应头字段
28             for (String key : map.keySet()) {
29                 System.out.println(key + "--->" + map.get(key));
30             }
31             // 定义 BufferedReader输入流来读取URL的响应
32             in = new BufferedReader(new InputStreamReader(
33                     connection.getInputStream()));
34             String line;
35             while ((line = in.readLine()) != null) {
36                 result += line;
37             }
38         } catch (Exception e) {
39             System.out.println("发送GET请求出现异常!" + e);
40             e.printStackTrace();
41         }
42         // 使用finally块来关闭输入流
43         finally {
44             try {
45                 if (in != null) {
46                     in.close();
47                 }
48             } catch (Exception e2) {
49                 e2.printStackTrace();
50             }
51         }
52         return result;
53     }

3.调用方法

1 //发送 POST 请求
2    String sr=post("http://localhost:6144/Home/RequestPostString", "key=123&v=456");
3    System.out.println(sr);
4 //发送 GET 请求
5     String s=get("http://localhost:6144/Home/RequestString", "key=123&v=456");
6     System.out.println(s);
7         

 

Java中发送http的get、post请求

标签:host   输出   关闭   name   stp   trace   tin   set   eth   

原文地址:http://www.cnblogs.com/evolutionoflicorice/p/7641768.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!