HTTP发送POST请求

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();
}
}
}

猜你喜欢

转载自blog.51cto.com/9381188/2324749