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

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

Introduction

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

Prototype

public void setHostnameVerifier(final X509HostnameVerifier hostnameVerifier) 

Source Link

Usage

From source file:hu.balazsbakai.sq.util.RestUtil.java

private DefaultHttpClient getNewTrustedHttpClient() {
    try {//ww  w .j av  a2  s  .co  m
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);

        SSLSocketFactory sf = new CustomTrustedSSLSocketFactory(trustStore);
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
        HttpConnectionParams.setConnectionTimeout(params, CONNECTION_TIMEOUT);
        HttpConnectionParams.setSoTimeout(params, SOCKET_TIMEOUT);

        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) {
        LogUtil.e("Exception", e);
        return new DefaultHttpClient();
    }
}

From source file:com.twotoasters.android.hoot.HootTransportHttpClient.java

@Override
public void setup(Hoot hoot) {
    HttpParams params = new BasicHttpParams();
    ConnManagerParams.setMaxTotalConnections(params, 10);
    ConnManagerParams.setTimeout(params, hoot.getTimeout());
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpConnectionParams.setConnectionTimeout(params, hoot.getTimeout());
    HttpConnectionParams.setSoTimeout(params, hoot.getTimeout());
    HttpConnectionParams.setTcpNoDelay(params, true);

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory();
    sslSocketFactory.setHostnameVerifier(hoot.getSSLHostNameVerifier());
    schemeRegistry.register(new Scheme("https", sslSocketFactory, 443));

    ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
    mClient = new DefaultHttpClient(cm, params);
    if (hoot.isBasicAuth()) {
        mClient.getCredentialsProvider().setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials(hoot.getBasicAuthUsername(), hoot.getBasicAuthPassword()));
    }//from   w w w  .  ja va 2 s .c o  m
}

From source file:com.googlesource.gerrit.plugins.hooks.rtc.network.RTCClient.java

private void setSSLTrustStrategy(boolean sslVerify) throws IOException {
    try {//from  w  w w.  j  av  a  2 s  . c o m
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }

            public void checkClientTrusted(X509Certificate[] certs, String authType) {
            }

            public void checkServerTrusted(X509Certificate[] certs, String authType) {
            }
        } };
        SSLContext sc;

        if (sslVerify) {
            sc = SSLContext.getDefault();
        } else {
            sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new SecureRandom());
        }

        SSLSocketFactory sf = new SSLSocketFactory(sc);
        sf.setHostnameVerifier(new AllowAllHostnameVerifier());
        SchemeRegistry schemeRegistry = httpclient.getConnectionManager().getSchemeRegistry();
        schemeRegistry.register(new Scheme("https", sf, 443));
    } catch (Exception any) {
        throw new IOException(any);
    }
}

From source file:info.semanticsoftware.semassist.android.intents.ServiceIntent.java

public String execute() {
    Log.d(Constants.TAG, "factory execute for " + pipelineName + " on server " + candidServerURL + " params "
            + RTParams + " input " + inputString);
    if (candidServerURL.indexOf("https") < 0) {
        Log.d(Constants.TAG, "non secure post to " + candidServerURL);
        RequestRepresentation request = new RequestRepresentation(SemAssistApp.getInstance(), pipelineName,
                RTParams, inputString);// w  w w . j a  v a2  s.c  om
        Representation representation = new StringRepresentation(request.getXML(), MediaType.APPLICATION_XML);
        Representation response = new ClientResource(candidServerURL).post(representation);
        String responseString = "";
        try {
            StringWriter writer = new StringWriter();
            response.write(writer);
            responseString = writer.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.d(Constants.TAG, "$$$ " + responseString);
        return responseString;
    } else {
        try {
            HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
            DefaultHttpClient client = new DefaultHttpClient();

            SchemeRegistry registry = new SchemeRegistry();
            final KeyStore ks = KeyStore.getInstance("BKS");
            // NOTE: the keystore must have been generated with BKS 146 and not later
            final InputStream in = SemAssistApp.getInstance().getContext().getResources()
                    .openRawResource(R.raw.clientkeystorenew);
            try {
                ks.load(in, SemAssistApp.getInstance().getContext().getString(R.string.keystorePassword)
                        .toCharArray());
            } finally {
                in.close();
            }

            SSLSocketFactory socketFactory = new CustomSSLSocketFactory(ks);
            socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
            registry.register(new Scheme("https", socketFactory, 443));
            SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry);
            DefaultHttpClient httpClient = new DefaultHttpClient(mgr, client.getParams());

            // Set verifier
            HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
            RequestRepresentation request = new RequestRepresentation(SemAssistApp.getInstance(), pipelineName,
                    RTParams, inputString);
            Representation representation = new StringRepresentation(request.getXML(),
                    MediaType.APPLICATION_XML);

            HttpPost post = new HttpPost(candidServerURL);
            post.setEntity(new StringEntity(representation.getText()));

            HttpResponse response = httpClient.execute(post);
            HttpEntity entity = response.getEntity();
            InputStream inputstream = entity.getContent();
            InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
            BufferedReader bufferedreader = new BufferedReader(inputstreamreader);

            String string = null;
            String responseString = "";
            while ((string = bufferedreader.readLine()) != null) {
                responseString += string;
            }
            return responseString;
        } catch (Exception e) {
            e.printStackTrace();
        }
    } //else
    return null;
}

From source file:com.googlesource.gerrit.plugins.its.rtc.network.RTCClient.java

private void setSSLTrustStrategy(boolean sslVerify) throws IOException {
    try {/*from www . j  ava 2s  .com*/
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }

            @Override
            public void checkClientTrusted(X509Certificate[] certs, String authType) {
            }

            @Override
            public void checkServerTrusted(X509Certificate[] certs, String authType) {
            }
        } };
        SSLContext sc;

        if (sslVerify) {
            sc = SSLContext.getDefault();
        } else {
            sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new SecureRandom());
        }

        SSLSocketFactory sf = new SSLSocketFactory(sc);
        sf.setHostnameVerifier(new AllowAllHostnameVerifier());
        SchemeRegistry schemeRegistry = httpclient.getConnectionManager().getSchemeRegistry();
        schemeRegistry.register(new Scheme("https", sf, 443));
    } catch (Exception any) {
        throw new IOException(any);
    }
}

From source file:groovyx.net.http.AuthConfig.java

/**
 * Sets a certificate to be used for SSL authentication.  See
 * {@link Class#getResource(String)} for how to get a URL from a resource
 * on the classpath.// w w  w. j av  a 2 s .  c  o m
 * @param certURL URL to a JKS keystore where the certificate is stored.
 * @param password password to decrypt the keystore
 */
public void certificate(String certURL, String password) throws GeneralSecurityException, IOException {

    KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
    InputStream jksStream = new URL(certURL).openStream();
    try {
        keyStore.load(jksStream, password.toCharArray());
    } finally {
        jksStream.close();
    }

    SSLSocketFactory ssl = new SSLSocketFactory(keyStore, password);
    ssl.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);

    builder.getClient().getConnectionManager().getSchemeRegistry().register(new Scheme("https", ssl, 443));
}

From source file:edu.cwru.apo.TrustAPOHttpClient.java

private SSLSocketFactory newSslSocketFactory() {
    try {/*www.j a  va2s .c o m*/
        // Get an instance of the Bouncy Castle KeyStore format
        KeyStore trusted = KeyStore.getInstance("BKS");
        // Get the raw resource, which contains the keystore with
        // your trusted certificates (root and any intermediate certs)
        InputStream in = context.getResources().openRawResource(R.raw.keystore);
        try {
            // Initialize the keystore with the provided trusted certificates
            // Also provide the password of the keystore
            trusted.load(in, "mysecret".toCharArray());
        } finally {
            in.close();
        }
        // Pass the keystore to the SSLSocketFactory. The factory is responsible
        // for the verification of the server certificate.
        SSLSocketFactory sf = new SSLSocketFactory(trusted);
        // Hostname verification from certificate
        // http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d4e506
        sf.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
        return sf;
    } catch (Exception e) {
        throw new AssertionError(e);
    }
}

From source file:com.phonty.improved.PhontyHttpClient.java

private SSLSocketFactory newSslSocketFactory() {
    try {/* ww w  . j a  v  a  2  s . com*/
        // Get an instance of the Bouncy Castle KeyStore format
        KeyStore trusted = KeyStore.getInstance("BKS");
        // Get the raw resource, which contains the keystore with
        // your trusted certificates (root and any intermediate certs)
        InputStream in = context.getResources().openRawResource(R.raw.keystore);
        try {
            // Initialize the keystore with the provided trusted certificates
            // Also provide the password of the keystore
            trusted.load(in, "pqoeponkjlcnvkjenenobnervoerovneokrnvoie".toCharArray());
        } finally {
            in.close();
        }
        // Pass the keystore to the SSLSocketFactory. The factory is responsible
        // for the verification of the server certificate.
        SSLSocketFactory sf = new SSLSocketFactory(trusted);
        // Hostname verification from certificate
        // http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d4e506
        sf.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
        return sf;
    } catch (Exception e) {
        throw new AssertionError(e);
    }
}

From source file:edu.rit.csh.androidwebnews.WebnewsHttpClient.java

/**
 * Makes the SSL cert work correctly.//  w  w  w .j  ava 2  s.com
 *
 * @return SSLSocketFactory - provides the SSLFactory for communicating
 *         with the scheme
 */
private SSLSocketFactory newSslSocketFactory() {
    try {
        // Get an instance of the Bouncy Castle KeyStore format
        KeyStore trusted = KeyStore.getInstance(KeyStore.getDefaultType());
        trusted.load(null, null);
        // Pass the keystore to the SSLSocketFactory. The factory is responsible
        // for the verification of the server certificate.
        SSLSocketFactory sf = new WebnewsSocketFactory(trusted);
        // Hostname verification from certificate
        // http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d4e506
        sf.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
        return sf;
    } catch (Exception e) {
        throw new AssertionError(e);
    }
}

From source file:net.openwatch.acluaz.http.AZHttpClient.java

private SSLSocketFactory newSslSocketFactory() {
    try {//from   w  w  w  . ja  va  2s.  c  om
        // Get an instance of the Bouncy Castle KeyStore format
        KeyStore trusted = KeyStore.getInstance("BKS");
        // Get the raw resource, which contains the keystore with
        // your trusted certificates (root and any intermediate certs)
        InputStream in = context.getResources().openRawResource(R.raw.azkeystore);
        try {
            // Initialize the keystore with the provided trusted certificates
            // Also provide the password of the keystore
            trusted.load(in, SECRETS.SSL_KEYSTORE_PASS.toCharArray());
        } finally {
            in.close();
        }
        // Pass the keystore to the SSLSocketFactory. The factory is responsible
        // for the verification of the server certificate.
        SSLSocketFactory sf = new SSLSocketFactory(trusted);
        // Hostname verification from certificate
        // http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d4e506
        sf.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
        return sf;
    } catch (Exception e) {
        throw new AssertionError(e);
    }
}