CloseableHttpClient发送http请求

String response = null;
// 客户端接口请求路径
String url = EspConfig.getClientBaseUrl() + ClientUtil.CLIENT_METHODNAME;
// 创建请求
CloseableHttpClient httpclient = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
// 配置请求
post.setHeader("Content-Type", "application/json;charset=UTF-8");
StringEntity stringEntity = new StringEntity(JSON.toJSONString(proScheduleVO), "utf-8");
stringEntity.setContentEncoding("utf-8");
stringEntity.setContentType("application/json");
post.setEntity(stringEntity);
try {
	// 执行请求获取响应
	 HttpResponse res = httpclient.execute(post);
	 log.info("======"+res.getStatusLine().getStatusCode());
	if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
	   response = EntityUtils.toString(res.getEntity());
	}
	 //判断返回是否为空
	if(response==null){
		throw new NullPointerException("推送失败");
	}
} catch (Exception e) {
	// TODO: handle exception
	e.printStackTrace();
}
// 转换成返回对象
log.info("======"+response);
JSONObject jsonObj = JSON.parseObject(response);
log.info("====="+jsonObj.get("resultCode").toString());
posted @ 2019-09-11 14:43  马大哈小果果  阅读(505)  评论(0)    收藏  举报