新建发送请求类:
/*
* 依赖
* <dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.10</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.12</version>
</dependency>
* */
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpRequest;
import org.apache.http.NoHttpResponseException;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.UnknownHostException;
import java.util.Map;
@Slf4j
public class httpPost {
private final static String url = "http://localhost:8001/process/";
private final static int maxPoolConnection = 2000;
private final static int MaxPoolConnectionPerRoute = 1000;
private final static PoolingHttpClientConnectionManager poolConnManager = new PoolingHttpClientConnectionManager(); //连接池管理器
private final static HttpRequestRetryHandler httpRequestRetryHandler = new HttpRequestRetryHandler() { //retry handler
public boolean retryRequest(IOException exception,
int executionCount, HttpContext context) {
if (executionCount >= 5) {
return false;
}
if (exception instanceof NoHttpResponseException) {
return true;
}
if (exception instanceof InterruptedIOException) {
return false;
}
if (exception instanceof UnknownHostException) {
return false;
}
if (exception instanceof ConnectTimeoutException) {
return false;
}
HttpClientContext clientContext = HttpClientContext
.adapt(context);
HttpRequest request = clientContext.getRequest();
if (!(request instanceof HttpEntityEnclosingRequest)) {
return true;
}
return false;
}
};
static { //类加载的时候 设置最大连接数 和 每个路由的最大连接数
poolConnManager.setMaxTotal(maxPoolConnection);
poolConnManager.setDefaultMaxPerRoute(MaxPoolConnectionPerRoute);
}
private static CloseableHttpClient getCloseableHttpClient() {
return HttpClients.custom()
.setConnectionManager(poolConnManager)
.setRetryHandler(httpRequestRetryHandler)
.build();
}
public static String post(String jsonReq){
return postJson(jsonReq);
}
public static String postJson(String message) {
log.info("【{}】==>" + message);
CloseableHttpClient httpClient = getCloseableHttpClient();
HttpPost httpPost = new HttpPost(url);
CloseableHttpResponse response = null;
String content = null;
try {
httpPost.setHeader("Accept", "application/json;charset=UTF-8");
httpPost.setHeader("Content-Type", "application/json");
StringEntity stringEntity = new StringEntity(message);
stringEntity.setContentType("application/json;charset=UTF-8");
httpPost.setEntity(stringEntity);
response = httpClient.execute(httpPost);
if (response.getStatusLine().getStatusCode() == 200) {
// 获取响应实体
HttpEntity resTestEntity = response.getEntity();
// 输出响应
content = EntityUtils.toString(resTestEntity, "utf-8");
}else {
System.out.println("无效请求");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (response != null) {
try {
EntityUtils.consume(response.getEntity());
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return content;
}
}
调用:
String result = httpPost.post(JSON.toJSONString(new student(“zhangsan”, 18));