Search in sources :

Example 51 with SchemeRegistry

use of org.apache.http.conn.scheme.SchemeRegistry in project 12306-hunter by xautlx.

the class HttpClientService method buildHttpClient.

/**
 * 构建HttpClient对象
 *
 * @return
 */
public static HttpClient buildHttpClient() {
    try {
        SSLContext sslcontext = SSLContext.getInstance("TLS");
        sslcontext.init(null, new TrustManager[] { tm }, null);
        SSLSocketFactory ssf = new SSLSocketFactory(sslcontext);
        ClientConnectionManager ccm = new DefaultHttpClient().getConnectionManager();
        SchemeRegistry sr = ccm.getSchemeRegistry();
        sr.register(new Scheme("https", 443, ssf));
        HttpParams params = new BasicHttpParams();
        params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 8000);
        params.setParameter(CoreConnectionPNames.SO_TIMEOUT, 8000);
        HttpClient httpclient = new DefaultHttpClient(ccm, params);
        httpclient.getParams().setParameter(HTTP.USER_AGENT, "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; BOIE9;ZHCN)");
        return httpclient;
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
}
Also used : BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) Scheme(org.apache.http.conn.scheme.Scheme) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) SSLContext(javax.net.ssl.SSLContext) SSLSocketFactory(org.apache.http.conn.ssl.SSLSocketFactory) BasicHttpParams(org.apache.http.params.BasicHttpParams) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) CertificateException(java.security.cert.CertificateException)

Example 52 with SchemeRegistry

use of org.apache.http.conn.scheme.SchemeRegistry in project platformlayer by platformlayer.

the class MetricClientImpl method buildHttpClient.

private HttpClient buildHttpClient(CertificateAndKey certificateAndKey, List<String> trustKeys) {
    int port = metricBaseUrl.getPort();
    if (port == -1) {
        String scheme = metricBaseUrl.getScheme();
        if (scheme.equals("https")) {
            port = 443;
        } else if (scheme.equals("http")) {
            port = 80;
        } else {
            throw new IllegalArgumentException("Unknown scheme: " + scheme);
        }
    }
    SchemeSocketFactory schemeSocketFactory;
    try {
        KeyManager keyManager = new SimpleClientCertificateKeyManager(certificateAndKey);
        TrustManager trustManager;
        X509HostnameVerifier hostnameVerifier;
        if (trustKeys != null) {
            trustManager = new PublicKeyTrustManager(trustKeys);
            hostnameVerifier = SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
        } else {
            trustManager = null;
            hostnameVerifier = SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER;
        }
        javax.net.ssl.SSLSocketFactory sslSocketFactory = SslHelpers.buildSslSocketFactory(keyManager, trustManager);
        schemeSocketFactory = new SSLSocketFactory(sslSocketFactory, hostnameVerifier);
    } catch (GeneralSecurityException e) {
        throw new IllegalArgumentException("Error building SSL client", e);
    }
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("https", port, schemeSocketFactory));
    PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager(schemeRegistry);
    HttpClient httpClient = new DefaultHttpClient(connectionManager);
    httpClient = new DecompressingHttpClient(httpClient);
    return httpClient;
}
Also used : SimpleClientCertificateKeyManager(com.fathomdb.crypto.SimpleClientCertificateKeyManager) PoolingClientConnectionManager(org.apache.http.impl.conn.PoolingClientConnectionManager) Scheme(org.apache.http.conn.scheme.Scheme) PublicKeyTrustManager(com.fathomdb.crypto.ssl.PublicKeyTrustManager) SchemeSocketFactory(org.apache.http.conn.scheme.SchemeSocketFactory) GeneralSecurityException(java.security.GeneralSecurityException) DecompressingHttpClient(org.apache.http.impl.client.DecompressingHttpClient) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) TrustManager(javax.net.ssl.TrustManager) PublicKeyTrustManager(com.fathomdb.crypto.ssl.PublicKeyTrustManager) X509HostnameVerifier(org.apache.http.conn.ssl.X509HostnameVerifier) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) DecompressingHttpClient(org.apache.http.impl.client.DecompressingHttpClient) HttpClient(org.apache.http.client.HttpClient) SSLSocketFactory(org.apache.http.conn.ssl.SSLSocketFactory) SimpleClientCertificateKeyManager(com.fathomdb.crypto.SimpleClientCertificateKeyManager) KeyManager(javax.net.ssl.KeyManager)

Example 53 with SchemeRegistry

use of org.apache.http.conn.scheme.SchemeRegistry in project OpenMEAP by OpenMEAP.

the class SSLUtils method getRelaxedSSLVerificationHttpClient.

public static HttpClient getRelaxedSSLVerificationHttpClient() {
    try {
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);
        SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, FormConstants.CHAR_ENC_DEFAULT);
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", sf, 443));
        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
        return new DefaultHttpClient(ccm, params);
    } catch (Exception e) {
        return new DefaultHttpClient();
    }
}
Also used : BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) Scheme(org.apache.http.conn.scheme.Scheme) ThreadSafeClientConnManager(org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) SSLSocketFactory(org.apache.http.conn.ssl.SSLSocketFactory) BasicHttpParams(org.apache.http.params.BasicHttpParams) KeyStore(java.security.KeyStore) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) KeyStoreException(java.security.KeyStoreException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) CertificateException(java.security.cert.CertificateException) UnknownHostException(java.net.UnknownHostException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 54 with SchemeRegistry

use of org.apache.http.conn.scheme.SchemeRegistry in project oxCore by GluuFederation.

the class SslDefaultHttpClient method createClientConnectionManager.

@Override
protected ClientConnectionManager createClientConnectionManager() {
    SchemeRegistry registry = new SchemeRegistry();
    registry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
    // Register for port 443 our SSLSocketFactory with our keystore to the
    // ConnectionManager
    registry.register(new Scheme("https", 443, newSslSocketFactory()));
    return new PoolingClientConnectionManager(registry);
}
Also used : PoolingClientConnectionManager(org.apache.http.impl.conn.PoolingClientConnectionManager) Scheme(org.apache.http.conn.scheme.Scheme) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry)

Example 55 with SchemeRegistry

use of org.apache.http.conn.scheme.SchemeRegistry in project coprhd-controller by CoprHD.

the class RenderProxy method createClientConnectionManager.

private static ClientConnectionManager createClientConnectionManager() {
    SchemeRegistry schemeRegistry = SchemeRegistryFactory.createDefault();
    SSLSocketFactory sf;
    if (StorageOsPlugin.isEnabled()) {
        try {
            // initialize an SSLContext with the vipr keystore and trustmanager.
            // This is basically a dup of most of the ViPRSSLSocketFactory constructor,
            // and could be extracted
            X509TrustManager[] trustManagers = { BourneUtil.getTrustManager() };
            KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
            kmf.init(BourneUtil.getKeyStore(), "".toCharArray());
            SSLContext context = SSLContext.getInstance("TLS");
            context.init(kmf.getKeyManagers(), trustManagers, new SecureRandom());
            sf = new SSLSocketFactory(context, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        } catch (Exception e) {
            throw new RuntimeException("Unable to initialize the ViPRX509TrustManager for RenderProxy", e);
        }
    } else {
        sf = new SSLSocketFactory(SSLUtil.getTrustAllContext(), SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    }
    Scheme httpsScheme = new Scheme("https", 443, sf);
    schemeRegistry.register(httpsScheme);
    ClientConnectionManager connectionManager = new PoolingClientConnectionManager(schemeRegistry);
    return connectionManager;
}
Also used : PoolingClientConnectionManager(org.apache.http.impl.conn.PoolingClientConnectionManager) Scheme(org.apache.http.conn.scheme.Scheme) X509TrustManager(javax.net.ssl.X509TrustManager) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) SecureRandom(java.security.SecureRandom) SSLContext(javax.net.ssl.SSLContext) SSLSocketFactory(org.apache.http.conn.ssl.SSLSocketFactory) PoolingClientConnectionManager(org.apache.http.impl.conn.PoolingClientConnectionManager) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager) IOException(java.io.IOException) UnexpectedException(play.exceptions.UnexpectedException) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Aggregations

SchemeRegistry (org.apache.http.conn.scheme.SchemeRegistry)91 Scheme (org.apache.http.conn.scheme.Scheme)88 SSLSocketFactory (org.apache.http.conn.ssl.SSLSocketFactory)58 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)50 ClientConnectionManager (org.apache.http.conn.ClientConnectionManager)48 BasicHttpParams (org.apache.http.params.BasicHttpParams)35 HttpParams (org.apache.http.params.HttpParams)33 ThreadSafeClientConnManager (org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager)30 SSLContext (javax.net.ssl.SSLContext)24 IOException (java.io.IOException)22 KeyManagementException (java.security.KeyManagementException)18 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)18 CertificateException (java.security.cert.CertificateException)15 HttpClient (org.apache.http.client.HttpClient)12 BasicClientConnectionManager (org.apache.http.impl.conn.BasicClientConnectionManager)12 PoolingClientConnectionManager (org.apache.http.impl.conn.PoolingClientConnectionManager)12 KeyStoreException (java.security.KeyStoreException)11 UnrecoverableKeyException (java.security.UnrecoverableKeyException)10 X509Certificate (java.security.cert.X509Certificate)10 X509TrustManager (javax.net.ssl.X509TrustManager)9