import com.alibaba.fastjson.JSONObject;
import fangrong.com.cn.constant.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.*;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.ConnectionKeepAliveStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
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.message.BasicNameValuePair;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import java.io.File;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.UnknownHostException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
@Slf4j
public class HttpUtil {
private HttpUtil() {
}
private final static PoolingHttpClientConnectionManager POOLCONNMANAGER = new PoolingHttpClientConnectionManager();
private final static HttpRequestRetryHandler HTTPREQUESTRETRYHANDLER = (exception, executionCount, context) -> {
if (executionCount >= 3) {
return false;
}
if (exception instanceof NoHttpResponseException) {
return true;
}
if (exception instanceof InterruptedIOException) {
return false;
}
if (exception instanceof UnknownHostException) {
return false;
}
HttpClientContext clientContext = HttpClientContext.adapt(context);
HttpRequest request = clientContext.getRequest();
return !(request instanceof HttpEntityEnclosingRequest);
};
And set the maximum number of connections for each route class loading when static {// maximum number of connections
POOLCONNMANAGER.setMaxTotal(500);
POOLCONNMANAGER.setDefaultMaxPerRoute(100);
}
private static RequestConfig getRequestConfig() {
return RequestConfig.custom()
.setSocketTimeout(10 * 1000)
.setConnectTimeout(10 * 1000)
.setConnectionRequestTimeout(10 * 1000)
.build();
}
private static CloseableHttpClient getCloseableHttpClient() {
ConnectionKeepAliveStrategy connectionKeepAliveStrategy = (httpResponse, httpContext) -> {
return 20 * 1000; // tomcat default keepAliveTimeout for the 20s
};
return HttpClients.custom()
.setDefaultRequestConfig(getRequestConfig())
.setConnectionManager(POOLCONNMANAGER)
.setRetryHandler(HTTPREQUESTRETRYHANDLER)
.setKeepAliveStrategy(connectionKeepAliveStrategy)
.build();
}
/**
* get
*
* @param url String
* @return String
*/
public static String get(String url) {
CloseableHttpClient httpClient = getCloseableHttpClient();
CloseableHttpResponse response = null;
try {
Charset charset = StandardCharsets.UTF_8;
HttpGet httpget = new HttpGet(url);
response = httpClient.execute(httpget);
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity, charset);
} catch (Exception e) {
log.error ( "Request Exception: {}", e);
} finally {
if (null != response) {
try {
response.close();
} catch (IOException e) {
log.error ( "Close resource failure", e);
}
}
}
return null;
}
}
2019 Unicorn Enterprises heavily recruiting engineers Python standard >>> Introduction 1. HttpClient HttpClient Is a subproject of Apache, it can provide support for the HTTP protocol Java cl...
At the beginning of the project, HttpConnection was used. Recently, in order to deal with the big promotion problem, we decided to change the request external map interface to httpclient connection po...
HttpClientUtils.java HttpClient tool class, initialize the request pool, provide Post get request HttpClientConnectionMonitorThread.java HttpClient daemon thread, regularly clean up zombie connections...
HTTP connection manager Manage connections and connection managers Http connection is a complex, stateful, thread-unsafe object, so it must be managed properly. An Http connection can only be accessed...
HTTPCLIENT Connection Pool Manager Test Get httpclient from the connection pool HTTPCLIENT from the connection pool second time...
Jar package required...
HTTPCLIENT connection pool settings illustrate Why httpclient needs to connect pool HTTPCLIENT connection pool configuration illustrate In the project, HTTPCLIENT is used in performance, and it is fou...
Phenomenon A SDK uploaded by the file is provided inside, and the file server is uploaded by the file server inside the company through the Apache httpclient, and then occasionally there will be colle...