java调用接口
...
private final static Logger LOGGER = LoggerFactory.getLogger(***.class);
...
public void obtain(Map<String,Object> param,String url){
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type", "application/json;charset=utf8");
String strJsonParam = JsonUtil.objectToJson(param);
CloseableHttpResponse response = null;
try {
StringEntity strEntity = new StringEntity(strJsonParam);
httpPost.setEntity(strEntity);
response = httpClient.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
String strResponseEntity = EntityUtils.toString(responseEntity);
JSONObject jsonObject = JSONObject.parseObject(strResponseEntity);
LOGGER.info("调用‘获取CIM接口’成功");
}catch (Exception e) {
LOGGER.error("调用‘获取CIM接口’出错:",e);
}finally {
try {
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (IOException e) {
LOGGER.error("释放资源出错:",e);
}
}
}