private String postHttpRequest(String objStr,String url){
HttpClient client = new DefaultHttpClient();
String bodyStr = "";
HttpPost httpPost = new HttpPost(url);
try {
StringEntity reqEntity = new StringEntity(objStr); //组装请求数据
httpPost.setEntity(reqEntity);
} catch (Exception e) {
log.error("postHttpRequest get entity error", e);
return bodyStr;
}
try {
HttpResponse response = client.execute(httpPost); //发送请求
HttpEntity entity = response.getEntity();
bodyStr = EntityUtils.toString(entity); //得到结果内容
}catch (Exception e) {
log.error("postHttpRequest execute post error", e);
return bodyStr;
}
log.info("bodyStr : " + bodyStr);
return bodyStr;
}
Httpclient POST
最新推荐文章于 2023-10-28 13:35:24 发布
本文介绍了一个使用Java实现的POST请求示例。该示例通过DefaultHttpClient发起HTTP POST请求,并将JSON格式的数据作为请求体发送到指定URL。文章详细展示了如何创建请求实体、设置请求参数及处理响应内容。
摘要由CSDN通过智能技术生成