Java Code Examples for org.apache.http.conn.ssl.SSLConnectionSocketFactory#getSocketFactory()

The following examples show how to use org.apache.http.conn.ssl.SSLConnectionSocketFactory#getSocketFactory() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpProtocolParent.java    From dubbox with Apache License 2.0 6 votes vote down vote up
private CloseableHttpClient createHttpClient(String hostname, int port) {
	ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory();
	LayeredConnectionSocketFactory sslsf = SSLConnectionSocketFactory.getSocketFactory();
	Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory> create()
			.register("http", plainsf).register("https", sslsf).build();
	PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);
	// 将最大连接数增加
	cm.setMaxTotal(maxTotal);
	// 将每个路由基础的连接增加
	cm.setDefaultMaxPerRoute(maxPerRoute);
	HttpHost httpHost = new HttpHost(hostname, port);
	// 将目标主机的最大连接数增加
	cm.setMaxPerRoute(new HttpRoute(httpHost), maxRoute);
	// 请求重试处理
	return HttpClients.custom().setConnectionManager(cm).setRetryHandler(httpRequestRetryHandler).build();
}
 
Example 2
Source File: HttpClientFactory.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
public HttpClientFactory build() {

            if (this.sslSocketFactory == null) {
                this.sslSocketFactory = SSLConnectionSocketFactory.getSocketFactory();
            }
            if (this.plainSocketFactory == null) {
                this.plainSocketFactory = PlainConnectionSocketFactory.getSocketFactory();
            }
            if (this.httpIOSessionStrategy == null) {
                this.httpIOSessionStrategy = NoopIOSessionStrategy.INSTANCE;
            }
            if (this.httpsIOSessionStrategy == null) {
                this.httpsIOSessionStrategy = SSLIOSessionStrategy.getSystemDefaultStrategy();
            }

            return new HttpClientFactory(this);
        }
 
Example 3
Source File: HttpProtocolParent.java    From dtsopensource with Apache License 2.0 6 votes vote down vote up
private CloseableHttpClient createHttpClient(String hostname, int port) {
    ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory();
    LayeredConnectionSocketFactory sslsf = SSLConnectionSocketFactory.getSocketFactory();
    Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory> create()
            .register("http", plainsf).register("https", sslsf).build();
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);
    // 将最大连接数增加
    cm.setMaxTotal(maxTotal);
    // 将每个路由基础的连接增加
    cm.setDefaultMaxPerRoute(maxPerRoute);
    HttpHost httpHost = new HttpHost(hostname, port);
    // 将目标主机的最大连接数增加
    cm.setMaxPerRoute(new HttpRoute(httpHost), maxRoute);
    // 请求重试处理
    return HttpClients.custom().setConnectionManager(cm).setRetryHandler(httpRequestRetryHandler).build();
}
 
Example 4
Source File: HttpClient.java    From data-prep with Apache License 2.0 6 votes vote down vote up
/**
 * @return the http connection manager.
 */
@Bean(destroyMethod = "shutdown")
public PoolingHttpClientConnectionManager getConnectionManager() {

    // fallback to default implementation
    if (sslSocketFactory == null) {
        sslSocketFactory = SSLConnectionSocketFactory.getSocketFactory();
    }

    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(RegistryBuilder
            .<ConnectionSocketFactory> create()
            .register("http", PlainConnectionSocketFactory.getSocketFactory())
            .register("https", sslSocketFactory)
            .build());

    connectionManager.setMaxTotal(maxPoolSize);
    connectionManager.setDefaultMaxPerRoute(maxPerRoute);
    return connectionManager;
}
 
Example 5
Source File: HttpClientBuilder.java    From wechat-sdk with Apache License 2.0 5 votes vote down vote up
private SSLConnectionSocketFactory buildSslConnectionSocketFactory() {
    try {
        return new SSLConnectionSocketFactory(createIgnoreVerifySsl()); // 优先绕过安全证书
    } catch (Exception e) {
        LOGGER.error("ssl connection fail", e);
    }
    return SSLConnectionSocketFactory.getSocketFactory();
}
 
Example 6
Source File: HttpHelper.java    From sputnik with Apache License 2.0 5 votes vote down vote up
@Nullable
public LayeredConnectionSocketFactory buildSSLSocketFactory(ConnectorDetails connectorDetails) {
    if (connectorDetails.isVerifySsl()) {
        return SSLConnectionSocketFactory.getSocketFactory();
    }
    try {
        SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, TRUST_ALL_STRATEGY).build();
        return new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
    } catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) {
        log.error("Error building SSL socket factory", e);
        throw new IllegalStateException(e);
    }
}
 
Example 7
Source File: ApacheHttpClient.java    From jiguang-java-client-common with MIT License 4 votes vote down vote up
public CloseableHttpClient createHttpClient(int maxTotal, int maxPerRoute, int maxRoute,
                                            String hostname, int port) {
    ConnectionSocketFactory plainsf = PlainConnectionSocketFactory
            .getSocketFactory();
    LayeredConnectionSocketFactory sslsf = SSLConnectionSocketFactory
            .getSocketFactory();
    Registry<ConnectionSocketFactory> registry = RegistryBuilder
            .<ConnectionSocketFactory>create().register("http", plainsf)
            .register("https", sslsf).build();
    _cm = new PoolingHttpClientConnectionManager(
            registry);
    // 将最大连接数增加
    _cm.setMaxTotal(maxTotal);
    // 将每个路由基础的连接增加
    _cm.setDefaultMaxPerRoute(maxPerRoute);
    HttpHost httpHost = new HttpHost(hostname, port);
    // 将目标主机的最大连接数增加
    _cm.setMaxPerRoute(new HttpRoute(httpHost), maxRoute);

    // 请求重试处理
    HttpRequestRetryHandler httpRequestRetryHandler = new HttpRequestRetryHandler() {
        public boolean retryRequest(IOException exception,
                                    int executionCount, HttpContext context) {
            if (executionCount >= _maxRetryTimes) {
                return false;
            }
            if (exception instanceof NoHttpResponseException) {// 如果服务器丢掉了连接,那么就重试
                return true;
            }
            if (exception instanceof SSLHandshakeException) {// 不要重试SSL握手异常
                return false;
            }
            if (exception instanceof InterruptedIOException) {// 超时
                return false;
            }
            if (exception instanceof UnknownHostException) {// 目标服务器不可达
                return false;
            }
            if (exception instanceof ConnectTimeoutException) {// 连接被拒绝
                return false;
            }
            if (exception instanceof SSLException) {// SSL握手异常
                return false;
            }

            HttpClientContext clientContext = HttpClientContext
                    .adapt(context);
            HttpRequest request = clientContext.getRequest();
            // 如果请求是幂等的,就再次尝试
            if (!(request instanceof HttpEntityEnclosingRequest)) {
                return true;
            }
            return false;
        }
    };

    CloseableHttpClient httpClient = HttpClients.custom()
            .setConnectionManager(_cm)
            .setRetryHandler(httpRequestRetryHandler).build();

    return httpClient;

}