1. 添加依赖项
<!-- lombok常用工具集 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- hutool常用工具集 -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.1.0</version>
</dependency>
<!-- fastjson工具集 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.59</version>
</dependency>
2. 封装工具类
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
@Slf4j
public class YesHttpUtil {
public String httpRequestGet (String url, Map<String,Object> paramsMap, Integer connectionTimeout) {
try {
HttpResponse response = HttpRequest.get(url)
.form(paramsMap)
.timeout(connectionTimeout)
.execute();
if(response.getStatus() == 200) {
String result = response.body();
return result;
} else {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
String errTime = dateFormat.format(new Date());
log.error(errTime + " response error(" + response.getStatus() + "):" + url);
return null;
}
} catch (Exception ex) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
String errTime = dateFormat.format(new Date());
log.error(errTime + " request shell CRM error: " + ex.getMessage());
return null;
}
}
public JSONObject httpRequestPost (String url, String jsonBody, Integer connectionTimeout) {
try {
HttpResponse response = HttpRequest.post(url)
.contentType("application/json")
.timeout(connectionTimeout)
.body(jsonBody)
.execute();
if(response.getStatus() == 200) {
String result = response.body();
com.alibaba.fastjson.JSONObject jsonResult = JSON.parseObject(result);
return jsonResult;
} else {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
String errTime = dateFormat.format(new Date());
log.error(errTime + " response error(" + response.getStatus() + "):" + url);
return null;
}
} catch (Exception ex){
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
String errTime = dateFormat.format(new Date());
log.error(errTime + " request shell CRM error: " + ex.getMessage());
return null;
}
}
public JSONObject httpRequestPut (String url, String jsonBody, Integer connectionTimeout) {
try {
HttpResponse response = HttpRequest.put(url)
.contentType("application/json")
.timeout(connectionTimeout)
.body(jsonBody)
.execute();
if(response.getStatus() == 200) {
String result = response.body();
com.alibaba.fastjson.JSONObject jsonResult = JSON.parseObject(result);
return jsonResult;
} else {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
String errTime = dateFormat.format(new Date());
log.error(errTime + " response error(" + response.getStatus() + "):" + url);
return null;
}
} catch (Exception ex){
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
String errTime = dateFormat.format(new Date());
log.error(errTime + " request shell CRM error: " + ex.getMessage());
return null;
}
}
public static Object httpClientPost(String url, Object object, Integer connectionTimeout, Integer connectionRequestTimeout, Integer socketTimeout) {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(connectionTimeout)
.setConnectionRequestTimeout(connectionRequestTimeout)
.setSocketTimeout(socketTimeout).build();
httpPost.setConfig(requestConfig);
httpPost.addHeader("Content-Type", "application/json; charset=utf-8");
String jsonString = JSONObject.toJSONString(object);
StringEntity entity = new StringEntity(jsonString, ContentType.APPLICATION_JSON);
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
try {
CloseableHttpResponse response = httpClient.execute(httpPost);
System.out.println("得到的结果:" + response.getStatusLine());
HttpEntity entityResult = response.getEntity();
String result = EntityUtils.toString(entityResult, "utf-8");
JSONObject jsonObject = JSONObject.parseObject(result);
System.out.println(jsonObject);
return jsonObject;
} catch (Exception ex) {
System.out.println("发送 POST 请求出现异常!" + ex);
ex.printStackTrace();
return null;
}
}
}