首頁 >Java >java教程 >Java的實作得到,PUT,POST,刪除請求

Java的實作得到,PUT,POST,刪除請求

巴扎黑
巴扎黑原創
2017-03-28 15:36:172598瀏覽

Java的實作得到,PUT,POST,刪除請求:

1,取得 

public static String doGet(String strUrl ){
    String strReturn=""; 
    HttpGet httpGet = new HttpGet(strUrl);
    CloseableHttpClient httpclient = null; 
    CloseableHttpResponse response1=null;
        try {
        httpclient = HttpClients.createDefault();
        response1 = httpclient.execute(httpGet); 
            HttpEntity entity1 = response1.getEntity(); 
            strReturn=EntityUtils.toString(entity1) ;
            EntityUtils.consume(entity1); 
        }catch(Exception e){ 
        e.printStackTrace();
        }finally {  
try {
if(response1!=null)
response1.close();
} catch (IOException e) { 
e.printStackTrace();
}
        }
    return strReturn;
    }

2,放

public static String doPut(String strUrl,String param){ 
     CloseableHttpClient httpclient = HttpClients.createDefault();
     StringBuffer jsonString= new StringBuffer();
         try {
         final HttpPut put=new HttpPut(strUrl);
         put.setEntity(new StringEntity(param,"UTF-8")); 
            CloseableHttpResponse response1= httpclient.execute(put ); 
             try {
                 HttpEntity entity1 = response1.getEntity();
                 BufferedReader br = new BufferedReader(new InputStreamReader(entity1.getContent())); 
                 String line;
                 while ((line = br.readLine()) != null) {
                         jsonString.append(line);
                 }  
                 
                 EntityUtils.consume(entity1);
             } finally {
                 response1.close();
             }
         }catch(Exception e){
         e.printStackTrace();
         }
         return jsonString.toString();
    }
reee

以上就是Java的實作得到,PUT,POST,刪除要求的內容,更多相關內容請關注PHP中文網(www.php.cn)! 相關文章:

詳細介紹Java程式設計常見問題摘要

Java多執行緒基礎詳解

Java遠端通訊技術及原理分析的圖文介紹

Java遠端通訊技術及原理分析的圖文介紹

Java遠端通訊技術及原理分析的圖文介紹

🎜🎜Java遠端
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:List排序下一篇:List排序