zoukankan      html  css  js  c++  java
  • Java中发送http的get、post请求

    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         
  • 相关阅读:
    Gmail邮件被屏蔽
    每天读两本书的方法
    如何做到一天读一本书?
    给网站加图标
    接口和类的异同
    生气的时候如何不生气
    只有某行文字间距较大
    视频流媒体监控系统EasyDSS是如何在无人机巡查秸秆焚烧中发挥作用的?
    互联网直播点播平台EasyDSS视频直播通道被占用了怎么处理?
    互联网直播点播平台EasyDSS如何实现电梯监控?EasyDSS电梯云物联解决方案介绍
  • 原文地址:https://www.cnblogs.com/evolutionoflicorice/p/7641768.html
Copyright © 2011-2022 走看看