首页 > 代码库 > java http请求get与post

java http请求get与post

  先放一个连接  http://www.techweb.com.cn/network/system/2016-10-11/2407736.shtml

 

  然后贴代码

  

  

public void sendGet(String reqUrl, String params, String contentType) throws Exception {
        StringBuffer result = new StringBuffer();
        BufferedReader reader;
        URLConnection conn;
        URL url = new URL(reqUrl);
        conn = url.openConnection();
        conn.setRequestProperty("accept", contentType);
        conn.setRequestProperty("content-type", contentType);
        conn.setRequestProperty("connection", "keep-live");
        //conn.setRequestProperty("authorization", "Bearer " + this.token);
        conn.connect();
        Map<String, List<String>> map = conn.getHeaderFields();
        for (String key : map.keySet()) {
                   System.out.println(key + "--->" + map.get(key));
                }
        reader = new BufferedReader(new     InputStreamReader(conn.getInputStream()));
        String line;
        while ((line = reader.readLine()) != null) {
            result.append(line);
        }
        reader.close();
        System.err.println(result);
        
    }            

以上是get方法

public void sendPost(String reqUrl, JSONObject body) throws Exception {
        StringBuffer result = new StringBuffer();
        PrintWriter writer;
        BufferedReader reader;
        URLConnection conn;
        URL url = new URL(reqUrl);
        conn = url.openConnection();
        conn.setRequestProperty("accept", "application/vnd.plcm.plcm-recording-info-list+json");
        conn.setRequestProperty("content-type", "application/vnd.plcm.plcm-recording-info-list+json");
        conn.setRequestProperty("connection", "keep-alive");
        //conn.setRequestProperty("user-agent",
            //    "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0");
        conn.setRequestProperty("Authorization", "Bearer " + this.token);
        conn.setDoOutput(true);
        conn.setDoInput(true);
        writer = new PrintWriter(conn.getOutputStream());
        if (body != null) {
            writer.write(body.toString());
        }
        writer.flush();
        conn.connect();
        Map<String, List<String>> map = conn.getHeaderFields();
        for (String key : map.keySet()) {
           System.out.println(key + "--->" + map.get(key));
        }
        reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        while ((line = reader.readLine()) != null) {
            result.append(line);
        }
        reader.close();
        System.err.println(result.toString());
    }

以上是post提交的代码

(请自动忽略请求头里的accept和content-type)

 

要说的是,之前在发送get请求时,不小心粘贴了

 writer = new PrintWriter(conn.getOutputStream());
        if (body != null) {
            writer.write(body.toString());
        }
        writer.flush();
这部分代码,服务端抛出了415 作为response code
unsupported media type
结合最开始链接,几乎可以相信,get只发一次包,而post发两次,通俗的讲,假设把一个httprequest看成是有header和body构成的话,get一次将header和body一并发送,而post先发送header,然后再发送body,
这个有待以后验证。

java http请求get与post