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