/**
* post请求
*
* @param uri
* @return
* @throws IOException
*/
public static HttpResponse doPost(String uri, String token) throws IOException {
// 创建httpClient对象
HttpClient httpclient = HttpClients.createDefault();
// 设置请求地址
HttpPost httppost = new HttpPost(uri);
// 设置请求头
httppost.addHeader("Content-Type", HttpConstants.HEADER_CONTENT_TYPE_JSON);
httppost.addHeader("Authorization", token);
// 执行http请求
return httpclient.execute(httppost);
}
/**
* post请求
*
* @param params 参数
* @param uri uri
* @return {@link HttpResponse}
*/
public static HttpResponse doPostReq(String params, String uri) throws IOException {
// 创建httpClient对象
HttpClient httpclient = HttpClients.createDefault();
// 设置请求地址
HttpPost httppost = new HttpPost(uri);
// 设置请求头
httppost.addHeader("Content-Type", HttpConstants.HEADER_CONTENT_TYPE_JSON);
// 设置请求内容
StringEntity se = new StringEntity(params, "utf-8");
httppost.setEntity(se);
// 执行http请求
return httpclient.execute(httppost);
}
public final class HttpConstants {
public static final String HEADER_CONTENT_TYPE_JSON = "application/json;charset=UTF-8";
public static final Integer SUCCESS_CODE = 200;
public HttpConstants() {}
}
private boolean toGetInventoryInfo(Map<String, Object> reqMap) throws IOException {
String params = JSON.toJSONStringWithDateFormat(reqMap,
DateUtil.FORMAT_DATE_TIME
, SerializerFeature.DisableCircularReferenceDetect);
String inventoryUrl = aeonProperties.getInventory();
log.info("请求,params:{},inventoryUrl:{}", params, inventoryUrl);
HttpResponse response = HttpUtils.doPostReq(params, inventoryUrl);
log.info("请求结果,response:{}", response.toString());
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String result = EntityUtils.toString(response.getEntity(), "utf-8");
log.info("请求数据结果,result:{}", result);
List<Map> maps = JSONObject.parseArray(result, Map.class);
if(!CollectionUtils.isEmpty(maps)){
Map<String, Object> map = maps.get(0);
return (Boolean) map.get("isRepertory");
}
}else {
log.info("请求失败,params:{},inventoryUrl:{}", params, inventoryUrl);
}
return false;
}