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

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

Introduction

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

Prototype

X509HostnameVerifier STRICT_HOSTNAME_VERIFIER

To view the source code for org.apache.http.conn.ssl SSLSocketFactory STRICT_HOSTNAME_VERIFIER.

Click Source Link

Usage

From source file:org.ellis.yun.search.test.httpclient.HttpClientTest.java

@SuppressWarnings("deprecation")
@Test/*w  w  w  .  j  a v a 2 s .co m*/
public void testSSLConnection() throws Exception {
    Scheme http = new Scheme("http", PlainSocketFactory.getSocketFactory(), 80);
    SSLSocketFactory ssf = new SSLSocketFactory(SSLContext.getInstance("TLS"));
    ssf.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
    Scheme https = new Scheme("https", ssf, 443);
    SchemeRegistry sr = new SchemeRegistry();
    sr.register(http);
    sr.register(https);

    TrustManager easyTrustManager = new X509TrustManager() {

        public void checkClientTrusted(java.security.cert.X509Certificate[] arg0, String arg1) {
            System.out.println("checkClientTrusted");
        }

        public void checkServerTrusted(java.security.cert.X509Certificate[] arg0, String arg1) {
            System.out.println("checkServerTrusted");
        }

        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            System.out.println("getAcceptedIssuers");
            return null;
        }
    };

    SSLContext sslcontext = SSLContext.getInstance("TLS");
    sslcontext.init(null, new TrustManager[] { easyTrustManager }, null);
    SSLSocketFactory sf = new SSLSocketFactory(sslcontext);
    SSLSocket socket = (SSLSocket) sf.createSocket();
    socket.setEnabledCipherSuites(new String[] { "SSL_RSA_WITH_RC4_128_MD5" });
    HttpParams params = new BasicHttpParams();
    params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 1000);
    sf.connectSocket(socket, "119.29.234.42", 443, null, -1, params);
}

From source file:com.mhise.util.MHISEUtil.java

public static DefaultHttpClient initializeHTTPClient(Context ctx, KeyStore localTrustStore) {
    DefaultHttpClient httpClient = null;
    try {//from   w ww .  j a v a 2  s  .  c o  m
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        SSLSocketFactory sslSocketFactory = new SSLSocketFactory(localTrustStore, null,
                getServerKeyStore(Constants.HTTPS_URL_SVC));
        sslSocketFactory.setHostnameVerifier((X509HostnameVerifier) SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
        schemeRegistry.register(new Scheme("https", sslSocketFactory, 443));
        HttpParams params = new BasicHttpParams();
        ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
        httpClient = new DefaultHttpClient(cm, params);

    } catch (Exception e) {
        // TODO: handle exception
        Logger.debug("MHISEUtil-->initializeHTTPClient -->", "" + e);
    }
    return httpClient;
}

From source file:com.lgallardo.qbittorrentclient.JSONParser.java

public DefaultHttpClient getNewHttpClient() {
    try {//from  w ww.  ja v  a2 s.  c o  m

        KeyStore localTrustStore = KeyStore.getInstance("BKS");

        InputStream in = null;

        try {

            localTrustStoreFile = new File(keystore_path);
            in = new FileInputStream(localTrustStoreFile);

            localTrustStore.load(in, keystore_password.toCharArray());
        } finally {
            if (in != null) {
                in.close();
            }
        }

        MySSLSocketFactory sf = new MySSLSocketFactory(localTrustStore);
        sf.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);

        HttpParams params = new BasicHttpParams();

        //            HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        //            HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

        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();
    }
}