码迷,mamicode.com
首页 > Web开发 > 详细

HTTP发送POST请求

时间:2018-12-01 18:25:46      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:异常   value   cat   app   exception   instance   结果   pos   throws   

public void postSend(String reqJsonData, String documentCode, String userId) throws Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
String requestUrl = ConfigurationCache.getInstance().getValue(ConfigurationCache.REQUEST_SERVICE_URL);
HttpPost httpPost = new HttpPost(requestUrl + "/testapi/test/send");
httpPost.setHeader("Content-Type", MediaType.APPLICATION_JSON_UTF8.toString());
httpPost.setEntity(new StringEntity(reqJsonData, "UTF-8"));
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpPost);
int errorCode = response.getStatusLine().getStatusCode();
logger.info("请求响应的状态:" + errorCode);
String responseResult = EntityUtils.toString(response.getEntity());
ResponseData responseData = XmlParseUtil.xmlToObj(responseResult, ResponseData.class, null);
logger.info("响应的结果:" + JSON.toJSONString(responseData));
response.close();
httpClient.close();
} catch (Exception e) {
logger.error("请求发生异常,原因:" , e);
if (response != null) {
response.close();
}
if (httpClient != null) {
httpClient.close();
}
}
}

HTTP发送POST请求

标签:异常   value   cat   app   exception   instance   结果   pos   throws   

原文地址:http://blog.51cto.com/9381188/2324749

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!