All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cc.kebei.expands.request.http.simple.SimpleHttpsRequest Maven / Gradle / Ivy

There is a newer version: 3.0.6
Show newest version
package cc.kebei.expands.request.http.simple;

import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.DefaultHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.ssl.TrustStrategy;

import javax.net.ssl.SSLContext;
import java.security.KeyStore;

public class SimpleHttpsRequest extends SimpleHttpRequest {

    public SimpleHttpsRequest(String url) {
        super(url);
        RegistryBuilder registryBuilder = RegistryBuilder.create();
        ConnectionSocketFactory plainSF = new PlainConnectionSocketFactory();
        registryBuilder.register("http", plainSF);
        SSLContext sslContext;
        try {
            KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
            //信任任何链接
            TrustStrategy anyTrustStrategy = (x509Certificates, s) -> true;
            sslContext = SSLContexts.custom().useProtocol("TLS").loadTrustMaterial(trustStore, anyTrustStrategy).build();
            LayeredConnectionSocketFactory sslSF = new SSLConnectionSocketFactory(sslContext,new DefaultHostnameVerifier());
            registryBuilder.register("https", sslSF);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        Registry registry = registryBuilder.build();
        //设置连接管理器
        setPool(new PoolingHttpClientConnectionManager(registry));
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy