httpclient post使用https 加连接池的工具类
时间: 2024-05-24 15:04:34 浏览: 98
httpclient 工具类
你可以使用 Apache HttpComponents 中的 HttpClient 来进行 https 请求,并使用连接池来提高性能。下面是一个示例代码:
```java
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
public class HttpsClientUtil {
private static final int MAX_TOTAL_CONNECTIONS = 200;
private static final int MAX_PER_ROUTE_CONNECTIONS = 20;
private static final int CONNECTION_TIMEOUT_MS = 5000;
private static final int SOCKET_TIMEOUT_MS = 10000;
private static CloseableHttpClient httpClient;
static {
try {
SSLContext sslContext = SSLContexts.custom().build();
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext,
new String[]{"TLSv1.2"}, null, SSLConnectionSocketFactory.getDefaultHostnameVerifier());
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("https", socketFactory)
.build();
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
connectionManager.setMaxTotal(MAX_TOTAL_CONNECTIONS);
connectionManager.setDefaultMaxPerRoute(MAX_PER_ROUTE_CONNECTIONS);
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(CONNECTION_TIMEOUT_MS)
.setSocketTimeout(SOCKET_TIMEOUT_MS)
.build();
httpClient = HttpClientBuilder.create()
.setConnectionManager(connectionManager)
.setDefaultRequestConfig(requestConfig)
.build();
} catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) {
throw new RuntimeException(e);
}
}
public static String post(String url, List<BasicNameValuePair> parameters) throws IOException {
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(parameters, StandardCharsets.UTF_8));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity, StandardCharsets.UTF_8);
}
}
```
使用方法:
```java
List<BasicNameValuePair> parameters = new ArrayList<>();
parameters.add(new BasicNameValuePair("key1", "value1"));
parameters.add(new BasicNameValuePair("key2", "value2"));
String response = HttpsClientUtil.post("https://example.com/api", parameters);
```
此处仅提供一个简单的示例,实际使用时需要根据具体情况进行调整和扩展。
阅读全文