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

java http请求get与post

时间:2016-12-11 01:24:04      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:reader   output   his   url   app   结合   span   ams   plc   

  先放一个连接  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

标签:reader   output   his   url   app   结合   span   ams   plc   

原文地址:http://www.cnblogs.com/dbein-lee/p/6158380.html

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