java類比post請求發送json

來源:互聯網
上載者:User

標籤:pac   pcl   info   .net   buffer   nes   system   disco   bytes   

java類比post請求發送json,用兩種方式實現,第一種是HttpURLConnection發送post請求,第二種是使用httpclient類比post請求,

方法一:

 1 package main.utils; 2  3 import java.io.*; 4 import java.net.HttpURLConnection; 5 import java.net.URL; 6  7 public class HttpUtilTest { 8     Log log = new Log(this.getClass());//初始化日誌類 9     /**10      * @作用 使用urlconnection11      * @param url12      * @param Params13      * @return14      * @throws IOException15      */16     public String sendPost(String url,String Params)throws IOException{17         OutputStreamWriter out = null;18         BufferedReader reader = null;19         String response="";20         try {21             URL httpUrl = null; //HTTP URL類 用這個類來建立串連22             //建立URL23             httpUrl = new URL(url);24             //建立串連25             HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();26             conn.setRequestMethod("POST");27             conn.setRequestProperty("Content-Type", "application/json");28             conn.setRequestProperty("connection", "keep-alive");29             conn.setUseCaches(false);//設定不要緩衝30             conn.setInstanceFollowRedirects(true);31             conn.setDoOutput(true);32             conn.setDoInput(true);33             conn.connect();34             //POST請求35             out = new OutputStreamWriter(36                     conn.getOutputStream());37             out.write(Params);38             out.flush();39             //讀取響應40             reader = new BufferedReader(new InputStreamReader(41                     conn.getInputStream()));42             String lines;43             while ((lines = reader.readLine()) != null) {44                 lines = new String(lines.getBytes(), "utf-8");45                 response+=lines;46             }47             reader.close();48             // 中斷連線49             conn.disconnect();50 51             log.info(response.toString());52         } catch (Exception e) {53         System.out.println("發送 POST 請求出現異常!"+e);54         e.printStackTrace();55         }56         //使用finally塊來關閉輸出資料流、輸入資料流57         finally{58         try{59             if(out!=null){60                 out.close();61             }62             if(reader!=null){63                 reader.close();64             }65         }66         catch(IOException ex){67             ex.printStackTrace();68         }69     }70 71         return response;72     }73 }

 

 方法二:使用httpclient實現

 1 //post要求方法 2     public  String sendPost(String url, String data) { 3         String response = null; 4         log.info("url: " + url); 5         log.info("request: " + data); 6         try { 7             CloseableHttpClient httpclient = null; 8             CloseableHttpResponse httpresponse = null; 9             try {10                 httpclient = HttpClients.createDefault();11                 HttpPost httppost = new HttpPost(url);12                 StringEntity stringentity = new StringEntity(data,13                         ContentType.create("text/json", "UTF-8"));14                 httppost.setEntity(stringentity);15                 httpresponse = httpclient.execute(httppost);16                 response = EntityUtils17                         .toString(httpresponse.getEntity());18                 log.info("response: " + response);19             } finally {20                 if (httpclient != null) {21                     httpclient.close();22                 }23                 if (httpresponse != null) {24                     httpresponse.close();25                 }26             }27         } catch (Exception e) {28             e.printStackTrace();29         }30         return response;31     }

 

 

 

java類比post請求發送json

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.