HttpClient,DefaultHttpClient使用


public static String XXXPost(String url, String json)throws Exception{

//創建一個DefaultHttpClient的實例

HttpClient httpClient = new DefaultHttpClient();

//創建一個HttpPost對象,傳入目標的網絡地址:

HttpPost post = new HttpPost(url);

//參數傳遞 

StringEntity postingString = new StringEntity(json);// json傳遞
postingString.setContentType("application/x-www-form-urlencoded");

//參數傳入 

post.setEntity(postingString);

//執行execute()方法之后會返回一個HttpResponse對象,服務器所返回的所有信息就保護在HttpResponse里面.

先取出服務器返回的狀態碼,如果等於200就說明請求和響應都成功了:

HttpResponse response = httpClient.execute(post);
HttpEntity entity = response.getEntity();
String content = EntityUtils.toString(entity);
return content;
}

下面是一位大神的詳細用法:

https://www.cnblogs.com/xiayahui/p/5623947.html 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM