Example usage for javax.net.ssl HttpsURLConnection setDefaultHostnameVerifier

List of usage examples for javax.net.ssl HttpsURLConnection setDefaultHostnameVerifier

Introduction

In this page you can find the example usage for javax.net.ssl HttpsURLConnection setDefaultHostnameVerifier.

Prototype

public static void setDefaultHostnameVerifier(HostnameVerifier v) 

Source Link

Document

Sets the default HostnameVerifier inherited by a new instance of this class.

Usage

From source file:com.gft.unity.android.AndroidIO.java

public void createHttpClients() throws NoSuchAlgorithmException, CertificateException, IOException,
        KeyStoreException, KeyManagementException, UnrecoverableKeyException {

    SSLSocketFactory socketFactory;
    SchemeRegistry registry = new SchemeRegistry();

    LOG.LogDebug(Module.PLATFORM, "Certificate Validation Enabled = " + this.Validatecertificates());

    if (this.Validatecertificates()) {
        HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
        // Set verifier
        HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);

        /******************************** 
         * USING DEFAULT ANDROID DEVICE SSLSocketFactory
         * the default factory was throwing errors verifying ssl certificates chains for some specific CA Authorities
         * (for example, Verisign root ceritificate G5 is not available on android devices <=2.3)
         * See more details on jira ticket [MOBPLAT-63]
         ******************************** 
        SSLSocketFactory socketFactory = SSLSocketFactory
              .getSocketFactory();//from  ww w . j ava2 s  .  c o  m
        socketFactory
              .setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
        */

        /*
          /******************************** 
         * USING VALIDATING SSLSocketFactory - Validating certificates per demand
         * See more details on jira ticket [MOBPLAT-63]
         ********************************
         */
        KeyStore trustStore;
        if (Build.VERSION.SDK_INT >= 14) {
            trustStore = KeyStore.getInstance("AndroidCAStore");
            trustStore.load(null, null);
        } else {
            try {
                trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
                ;
                String filename = "/system/etc/security/cacerts.bks".replace('/', File.separatorChar);
                FileInputStream is = new FileInputStream(filename);
                trustStore.load(is, "changeit".toCharArray());
                is.close();
            } catch (Exception ex) {
                try {
                    /*
                      /******************************** 
                     * HTC 2.3.5 Access Keystore problem
                     * See more details on jira ticket [MOBPLAT-91]
                     ********************************
                     */
                    trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
                    String filename = "/system/etc/security/cacerts.bks".replace('/', File.separatorChar);
                    FileInputStream is = new FileInputStream(filename);
                    trustStore.load(is, null);
                    is.close();
                } catch (Exception e) {
                    trustStore = null;
                    LOG.Log(Module.PLATFORM, "A problem has been detected while accessing the device keystore.",
                            e);
                }
            }
        }
        socketFactory = ValidatingSSLSocketFactory.GetInstance(trustStore);
        socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);

        LOG.LogDebug(Module.PLATFORM, "Using ValidatingSSLSocketFactory (custom socket Factory)");

    } else {
        /*
         * ******************************* 
         * USING CUSTOM SSLSocketFactory - accept all certificates
         * See more details on jira ticket [MOBPLAT-63]
         ********************************
        */
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);
        socketFactory = new MySSLSocketFactory(trustStore);

        LOG.LogDebug(Module.PLATFORM,
                "Using MySSLSocketFactory (custom socket factory - accepting all certificates)");
    }

    registry.register(new Scheme("https", socketFactory, 443));
    registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    ThreadSafeClientConnManager mgr = new ThreadSafeClientConnManager(new DefaultHttpClient().getParams(),
            registry);
    httpSSLClient = new DefaultHttpClient(mgr, new DefaultHttpClient().getParams());

    // [MOBPLAT-200] : allow gzip, deflate decompression modes
    httpSSLClient.addResponseInterceptor(new GzipHttpResponseInterceptor());

    LOG.LogDebug(Module.PLATFORM, "httpSSLClient stored for next HTTPS access");

}

From source file:org.openymsg.network.Session.java

private void trustEveryone() {
    try {/*from  w  ww  .  jav a 2 s.c om*/
        HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(final String hostname, final SSLSession session) {
                return true;
            }
        });
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, new X509TrustManager[] { new X509TrustManager() {
            @Override
            public void checkClientTrusted(final X509Certificate[] chain, final String authType)
                    throws CertificateException {
            }

            @Override
            public void checkServerTrusted(final X509Certificate[] chain, final String authType)
                    throws CertificateException {
            }

            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }
        } }, new SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
    } catch (Exception e) { // should never happen
        e.printStackTrace();
    }
}