Example usage for org.apache.http.conn.ssl SSLSocketFactory SSLSocketFactory

List of usage examples for org.apache.http.conn.ssl SSLSocketFactory SSLSocketFactory

Introduction

In this page you can find the example usage for org.apache.http.conn.ssl SSLSocketFactory SSLSocketFactory.

Prototype

public SSLSocketFactory(final SSLContext sslContext) 

Source Link

Usage

From source file:com.upyun.sdk.utils.HttpClientUtils.java

@SuppressWarnings("deprecation")
public static HttpClient getInstance() {
    HttpClient client = new DefaultHttpClient();
    SSLContext ctx = null;/*from   w ww .  j av  a  2 s  . c  o  m*/
    try {
        ctx = SSLContext.getInstance("TLS");
        ctx.init(null, new TrustManager[] { tm }, null);
    } catch (Exception e) {
        LogUtil.exception(logger, e);
    }
    SSLSocketFactory ssf = new SSLSocketFactory(ctx);
    ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    ClientConnectionManager ccm = client.getConnectionManager();
    SchemeRegistry sr = ccm.getSchemeRegistry();
    sr.register(new Scheme("https", ssf, 443));
    client = new DefaultHttpClient(ccm, client.getParams());
    return client;
}

From source file:org.jssec.android.https.privatecertificate.PrivateCertificateHttpsGet.java

@Override
protected Object doInBackground(String... params) {

    DefaultHttpClient client = new DefaultHttpClient();

    try {/*from   www.  j  a  v  a 2s .  c o m*/
        // ?1 ???
        // assets??????????KeyStoreclient?
        KeyStore ks = KeyStoreUtil.getEmptyKeyStore();
        KeyStoreUtil.loadX509Certificate(ks, mContext.getResources().getAssets().open("cacert.crt"));
        Scheme sch = new Scheme("https", new SSLSocketFactory(ks), 443);
        client.getConnectionManager().getSchemeRegistry().register(sch);

        // ?2 URI?https://??
        // ?3 ???????
        HttpGet request = new HttpGet(params[0]);
        HttpResponse response = client.execute(request);
        checkResponse(response);

        // ?4 ?????????
        return EntityUtils.toByteArray(response.getEntity());
    } catch (SSLException e) {
        // ?5 SSLException?????????
        // ??????
        return e;
    } catch (Exception e) {
        return e;
    } finally {
        // ?HttpClientshutdown?
        client.getConnectionManager().shutdown();
    }
}

From source file:de.koczewski.maxapi.WebClientDevWrapper.java

public static DefaultHttpClient wrapClient(HttpClient base) {
    try {/*from  www. j  ava 2  s .c o  m*/
        SSLContext ctx = SSLContext.getInstance("TLS");
        X509TrustManager tm = new X509TrustManager() {

            public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
            }

            public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
            }

            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        };
        X509HostnameVerifier verifier = new X509HostnameVerifier() {

            @Override
            public void verify(String string, SSLSocket ssls) throws IOException {
            }

            @Override
            public void verify(String string, X509Certificate xc) throws SSLException {
            }

            @Override
            public void verify(String string, String[] strings, String[] strings1) throws SSLException {
            }

            @Override
            public boolean verify(String string, SSLSession ssls) {
                return true;
            }
        };
        ctx.init(null, new TrustManager[] { tm }, null);
        SSLSocketFactory ssf = new SSLSocketFactory(ctx);
        ssf.setHostnameVerifier(verifier);
        ClientConnectionManager ccm = base.getConnectionManager();
        SchemeRegistry sr = ccm.getSchemeRegistry();
        sr.register(new Scheme("https", ssf, 443));
        return new DefaultHttpClient(ccm, base.getParams());
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:android.net.http.HttpsThroughHttpProxyTest.java

public void testConnectViaHttps() throws IOException, InterruptedException {
    TestSSLContext testSSLContext = TestSSLContext.create();

    MockWebServer server = new MockWebServer();
    server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
    server.enqueue(new MockResponse().setResponseCode(200).setBody("this response comes via HTTPS"));
    server.play();//from   w w w  . j av a2 s. c om

    HttpClient httpClient = new DefaultHttpClient();
    SSLSocketFactory sslSocketFactory = new SSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
    sslSocketFactory.setHostnameVerifier(new AllowAllHostnameVerifier());
    httpClient.getConnectionManager().getSchemeRegistry()
            .register(new Scheme("https", sslSocketFactory, server.getPort()));

    HttpResponse response = httpClient.execute(new HttpGet("https://localhost:" + server.getPort() + "/foo"));
    assertEquals("this response comes via HTTPS", contentToString(response));

    RecordedRequest request = server.takeRequest();
    assertEquals("GET /foo HTTP/1.1", request.getRequestLine());
}

From source file:wsattacker.http.transport.TlsWrapperClient.java

public static HttpClient wrapClient(HttpClient base) {
    try {//from  w  ww  .  j a va 2 s . c o m
        SSLContext ctx = SSLContext.getInstance("TLS");
        X509TrustManager tm = new X509TrustManager() {
            public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
            }

            public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
            }

            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        };
        X509HostnameVerifier verifier = new X509HostnameVerifier() {
            @Override
            public void verify(String string, X509Certificate xc) throws SSLException {
            }

            @Override
            public void verify(String string, String[] strings, String[] strings1) throws SSLException {
            }

            @Override
            public boolean verify(String string, SSLSession ssls) {
                return true;
            }

            @Override
            public void verify(String string, SSLSocket ssls) throws IOException {
            }
        };
        ctx.init(null, new TrustManager[] { tm }, null);
        SSLSocketFactory ssf = new SSLSocketFactory(ctx);
        ssf.setHostnameVerifier(verifier);
        ClientConnectionManager ccm = base.getConnectionManager();
        SchemeRegistry sr = ccm.getSchemeRegistry();
        sr.register(new Scheme("https", ssf, 443));
        return new DefaultHttpClient(ccm, base.getParams());
    } catch (NoSuchAlgorithmException ex) {
        return null;
    } catch (KeyManagementException ex) {
        return null;
    }
}

From source file:com.cloud.network.utils.HttpClientWrapper.java

public static HttpClient wrapClient(HttpClient base) {
    try {//from   w  ww  .  j ava 2 s  .co  m
        SSLContext ctx = SSLUtils.getSSLContext();
        X509TrustManager tm = new X509TrustManager() {

            @Override
            public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
            }

            @Override
            public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
            }

            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        };
        X509HostnameVerifier verifier = new X509HostnameVerifier() {

            @Override
            public void verify(String string, SSLSocket ssls) throws IOException {
            }

            @Override
            public void verify(String string, X509Certificate xc) throws SSLException {
            }

            @Override
            public void verify(String string, String[] strings, String[] strings1) throws SSLException {
            }

            @Override
            public boolean verify(String string, SSLSession ssls) {
                return true;
            }
        };
        ctx.init(null, new TrustManager[] { tm }, null);
        SSLSocketFactory ssf = new SSLSocketFactory(ctx);
        ssf.setHostnameVerifier(verifier);
        ClientConnectionManager ccm = base.getConnectionManager();
        SchemeRegistry sr = ccm.getSchemeRegistry();
        sr.register(new Scheme("https", ssf, 443));
        return new DefaultHttpClient(ccm, base.getParams());
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
}

From source file:com.villemos.ispace.httpcrawler.HttpClientConfigurer.java

public static HttpClient setupClient(boolean ignoreAuthenticationFailure, String domain, Integer port,
        String proxyHost, Integer proxyPort, String authUser, String authPassword, CookieStore cookieStore)
        throws NoSuchAlgorithmException, KeyManagementException {

    DefaultHttpClient client = null;/* w  ww .j  a v a 2s.  co m*/

    /** Always ignore authentication protocol errors. */
    if (ignoreAuthenticationFailure) {
        SSLContext sslContext = SSLContext.getInstance("SSL");

        // set up a TrustManager that trusts everything
        sslContext.init(null, new TrustManager[] { new EasyX509TrustManager() }, new SecureRandom());

        SchemeRegistry schemeRegistry = new SchemeRegistry();

        SSLSocketFactory sf = new SSLSocketFactory(sslContext);
        Scheme httpsScheme = new Scheme("https", sf, 443);
        schemeRegistry.register(httpsScheme);

        SocketFactory sfa = new PlainSocketFactory();
        Scheme httpScheme = new Scheme("http", sfa, 80);
        schemeRegistry.register(httpScheme);

        HttpParams params = new BasicHttpParams();
        ClientConnectionManager cm = new SingleClientConnManager(params, schemeRegistry);

        client = new DefaultHttpClient(cm, params);
    } else {
        client = new DefaultHttpClient();
    }

    if (proxyHost != null && proxyPort != null) {
        HttpHost proxy = new HttpHost(proxyHost, proxyPort);
        client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    } else {
        ProxySelectorRoutePlanner routePlanner = new ProxySelectorRoutePlanner(
                client.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault());
        client.setRoutePlanner(routePlanner);
    }

    /** The target location may demand authentication. We setup preemptive authentication. */
    if (authUser != null && authPassword != null) {
        client.getCredentialsProvider().setCredentials(new AuthScope(domain, port),
                new UsernamePasswordCredentials(authUser, authPassword));
    }

    /** Set default cookie policy and store. Can be overridden for a specific method using for example;
     *    method.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY); 
     */
    client.setCookieStore(cookieStore);
    // client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2965);      
    client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BEST_MATCH);

    return client;
}

From source file:org.apache.reef.runtime.hdinsight.client.sslhacks.UnsafeClientConstructor.java

@Override
public CloseableHttpClient newInstance() {
    try {//  ww w.j  a v  a 2  s .  co m
        final SSLSocketFactory socketFactory = new SSLSocketFactory(this.getSSLContext());
        socketFactory.setHostnameVerifier(new UnsafeHostNameVerifier());
        final SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("https", 443, socketFactory));
        final ClientConnectionManager clientConnectionManager = new BasicClientConnectionManager(
                schemeRegistry);
        return new DefaultHttpClient(clientConnectionManager);
    } catch (final KeyManagementException | NoSuchAlgorithmException ex) {
        throw new RuntimeException("Unable to instantiate HTTP Client", ex);
    }
}

From source file:ee.vvk.ivotingverification.util.CustomHttpsClient.java

private SSLSocketFactory newSslSocketFactory() {
    KeyStore trustStore;/*  w ww. java  2  s.co  m*/
    try {

        trustStore = Util.loadTrustStore((Activity) context);

        SSLSocketFactory sf = new SSLSocketFactory(trustStore);
        sf.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);

        return sf;
    } catch (Exception e) {
        if (Util.DEBUGGABLE) {
            Log.e(TAG, "Tehniline viga: " + e.getMessage(), e);
        }
        Util.startErrorIntent((Activity) context, C.badServerResponseMessage, true);
    }

    return null;
}

From source file:mobisocial.musubi.util.CertifiedHttpClient.java

private SSLSocketFactory newSslSocketFactory() {
    try {//from  w  w w .j  av a2  s. c o  m
        KeyStore trusted = KeyStore.getInstance("BKS");
        InputStream in = mContext.getResources().openRawResource(R.raw.servercertificates);
        try {
            trusted.load(in, "ez24get".toCharArray());
        } finally {
            in.close();
        }
        SSLSocketFactory sf = new SSLSocketFactory(trusted);
        //don't check the host name because we are doing funny redirects.  the
        //actual cert is good enough because it is bundled.
        sf.setHostnameVerifier(new AllowAllHostnameVerifier());
        return sf;
    } catch (Exception e) {
        throw new AssertionError(e);
    }
}