A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

[我的日记] Java发送post请求

© 庭院深深深几许 金牌黑马   /  2019-4-18 15:24  /  1868 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

Java发送post请求

20170929095505410.jpg

[Java] 纯文本查看 复制代码
package com.ddtech.app.util;
  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 
 12 import com.alibaba.fastjson.JSONObject;
 13 
 14 public class HttpRequestGetAndPost {
 15     /**
 16      * 向指定URL发送GET方法的请求
 17      * 
 18      * @param url
 19      *            发送请求的URL
 20      * @param param
 21      *            请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
 22      * @return URL 所代表远程资源的响应结果
 23      */
 24     public static String sendGet(String url, String param) {
 25         String result = "";
 26         BufferedReader in = null;
 27         try {
 28             String urlNameString = url + "?" + param;
 29             URL realUrl = new URL(urlNameString);
 30             // 打开和URL之间的连接
 31             URLConnection connection = realUrl.openConnection();
 32             // 设置通用的请求属性
 33             connection.setRequestProperty("accept", "*/*");
 34             connection.setRequestProperty("connection", "Keep-Alive");
 35             connection.setRequestProperty("user-agent",
 36                     "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
 37             // 建立实际的连接
 38             connection.connect();
 39             // 获取所有响应头字段
 40             Map> map = connection.getHeaderFields();
 41             // 遍历所有的响应头字段
 42             for (String key : map.keySet()) {
 43                 System.out.println(key + "--->" + map.get(key));
 44             }
 45             // 定义 BufferedReader输入流来读取URL的响应
 46             in = new BufferedReader(new InputStreamReader(
 47                     connection.getInputStream()));
 48             String line;
 49             while ((line = in.readLine()) != null) {
 50                 result += line;
 51             }
 52         } catch (Exception e) {
 53             System.out.println("发送GET请求出现异常!" + e);
 54             e.printStackTrace();
 55         }
 56         // 使用finally块来关闭输入流
 57         finally {
 58             try {
 59                 if (in != null) {
 60                     in.close();
 61                 }
 62             } catch (Exception e2) {
 63                 e2.printStackTrace();
 64             }
 65         }
 66         return result;
 67     }
 68 
 69     /**
 70      * 向指定 URL 发送POST方法的请求
 71      * 
 72      * @param url
 73      *            发送请求的 URL
 74      * @param param
 75      *            请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
 76      * @return 所代表远程资源的响应结果
 77      */
 78     public static String sendPost(String url, JSONObject param) {
 79         PrintWriter out = null;
 80         BufferedReader in = null;
 81         String result = "";
 82         try {
 83             URL realUrl = new URL(url);
 84             // 打开和URL之间的连接
 85             URLConnection conn = realUrl.openConnection();
 86             // 设置通用的请求属性
 87             conn.setRequestProperty("accept", "*/*");
 88             conn.setRequestProperty("connection", "Keep-Alive");
 89             conn.setRequestProperty("user-agent",
 90                     "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
 91             // 发送POST请求必须设置如下两行
 92             conn.setDoOutput(true);
 93             conn.setDoInput(true);
 94             // 获取URLConnection对象对应的输出流
 95             out = new PrintWriter(conn.getOutputStream());
 96             // 发送请求参数
 97             out.print(param);
 98             // flush输出流的缓冲
 99             out.flush();
100             // 定义BufferedReader输入流来读取URL的响应
101             in = new BufferedReader(
102                     new InputStreamReader(conn.getInputStream()));
103             String line;
104             while ((line = in.readLine()) != null) {
105                 result += line;
106             }
107         } catch (Exception e) {
108             System.out.println("发送 POST 请求出现异常!" + e);
109             e.printStackTrace();
110         }
111         // 使用finally块来关闭输出流、输入流
112         finally {
113             try {
114                 if (out != null) {
115                     out.close();
116                 }
117                 if (in != null) {
118                     in.close();
119                 }
120             } catch (IOException ex) {
121                 ex.printStackTrace();
122             }
123         }
124         return result;
125     }
126 }

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马