java http請求get與post

來源:互聯網
上載者:User

標籤: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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.