java后台http請求方式


長時間沒有寫這方面的代碼,突然提筆對於http的請求方式代碼不知道如何下手,特此記錄一下

 
         
public JSONObject  doPost(String url) throws IOException {
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(url);

postMethod.addRequestHeader("accept", "*/*");
postMethod.addRequestHeader("connection", "Keep-Alive");
//設置json格式傳送
postMethod.addRequestHeader("Content-Type", "application/json;charset=utf-8");
//必須設置下面這個Header
postMethod.addRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");
//添加請求參數
JSONObject jsonObject = new JSONObject();
jsonObject.put("url", "www.baidu.com");
RequestEntity se = new StringRequestEntity(jsonObject.toString(), "application/json", "utf-8");
postMethod.setRequestEntity(se);
httpClient.executeMethod(postMethod);
String responseMsg = postMethod.getResponseBodyAsString().trim();
JSONObject returnJson = JSONObject.parseObject(responseMsg);
return returnJson;
}
 
        

http請求方式有多種,參考鏈接:https://www.cnblogs.com/hhhshct/p/8523697.html


免責聲明!

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



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