CloseableHttpClient,HttpClient4使用配置

使用依赖如下:


    org.apache.httpcomponents
    httpmime
    4.5.14

 CloseableHttpClient 基本实现代码如下:

public class HttpClientConfig {

    private final static Logger logger = LoggerFactory.getLogger(HttpClientConfig.class);

    private static PoolingHttpClientConnectionManager poolConnManager = null;
    private static CloseableHttpClient defaultHttpClient;
    //请求器的配置
    private static RequestConfig requestConfig;

    static {

        try {
            logger.info("初始化HttpClient开始");
            SSLContextBuilder builder = new SSLContextBuilder();
            builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
                    builder.build());
            // 配置同时支持 HTTP 和 HTPPS
            Registry socketFactoryRegistry = RegistryBuilder.create().register(
                    "http", PlainConnectionSocketFactory.getSocketFactory()).register(
                    "https", sslsf).build();
            // 初始化连接管理器
            poolConnManager = new PoolingHttpClientConnectionManager(
                    socketFactoryRegistry);
            // 将最大连接数增加到200
            poolConnManager.setMaxTotal(200);
            // 设置最大路由[ 设置每个路由基础的连接],该值默认为2,并发比较差
            poolConnManager.setDefault

你可能感兴趣的:(http,java,java)