发送Http

  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 sendPost(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             } catch (IOException ex) {
 53                 ex.printStackTrace();
 54             }
 55         }
 56         return result;
 57     }
 58     /**
 59      * 向指定URL发送GET方法的请求
 60      *
 61      * @param url
 62      *            发送请求的URL
 63      * @param param
 64      *            请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
 65      * @return URL 所代表远程资源的响应结果
 66      */
 67     public static String sendGet(String url, String param) {
 68         String result = "";
 69         BufferedReader in = null;
 70         try {
 71             String urlNameString = url + "?" + param;
 72             URL realUrl = new URL(urlNameString);
 73             // 打开和URL之间的连接
 74             URLConnection connection = realUrl.openConnection();
 75             // 设置通用的请求属性
 76             connection.setRequestProperty("accept", "*/*");
 77             connection.setRequestProperty("connection", "Keep-Alive");
 78             connection.setRequestProperty("user-agent",
 79                     "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
 80             // 建立实际的连接
 81             connection.connect();
 82             // 获取所有响应头字段
 83             Map<String, List<String>> map = connection.getHeaderFields();
 84             // 遍历所有的响应头字段
 85             for (String key : map.keySet()) {
 86                 System.out.println(key + "--->" + map.get(key));
 87             }
 88             // 定义 BufferedReader输入流来读取URL的响应
 89             in = new BufferedReader(new InputStreamReader(
 90                     connection.getInputStream()));
 91             String line;
 92             while ((line = in.readLine()) != null) {
 93                 result += line;
 94             }
 95         } catch (Exception e) {
 96             System.out.println("发送GET请求出现异常!" + e);
 97             e.printStackTrace();
 98         }
 99         // 使用finally块来关闭输入流
100         finally {
101             try {
102                 if (in != null) {
103                     in.close();
104                 }
105             } catch (Exception e2) {
106                 e2.printStackTrace();
107             }
108         }
109         return result;
110     }
原文地址:https://www.cnblogs.com/duanxianyouyang/p/6553638.html