Example usage for com.squareup.okhttp OkHttpClient setSslSocketFactory

List of usage examples for com.squareup.okhttp OkHttpClient setSslSocketFactory

Introduction

In this page you can find the example usage for com.squareup.okhttp OkHttpClient setSslSocketFactory.

Prototype

public OkHttpClient setSslSocketFactory(SSLSocketFactory sslSocketFactory) 

Source Link

Document

Sets the socket factory used to secure HTTPS connections.

Usage

From source file:themeable.images.HttpsTrustManager.java

License:Apache License

public static OkHttpClient getTrustAllHttpClient() {
    try {// w  w w  .  jav a2  s .  c om
        // Create a trust manager that does not validate certificate chains
        final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            @Override
            public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType)
                    throws CertificateException {
            }

            @Override
            public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType)
                    throws CertificateException {
            }

            @Override
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        } };

        // Install the all-trusting trust manager
        final SSLContext sslContext = SSLContext.getInstance("SSL");
        sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
        // Create an ssl socket factory with our all-trusting manager
        final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

        OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient.setSslSocketFactory(sslSocketFactory);
        okHttpClient.setHostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });

        return okHttpClient;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:zblibrary.demo.manager.HttpRequest.java

License:Apache License

/**
 * @param url/* ww w.  j a  v  a2s. c o m*/
 * @return
 */
private OkHttpClient getHttpClient(String url) {
    Log.i(TAG, "getHttpClient  url = " + url);
    if (StringUtil.isNotEmpty(url, true) == false) {
        Log.e(TAG, "getHttpClient  StringUtil.isNotEmpty(url, true) == false >> return null;");
        return null;
    }

    OkHttpClient client = new OkHttpClient();
    client.setCookieHandler(new HttpHead());
    client.setConnectTimeout(15, TimeUnit.SECONDS);
    client.setWriteTimeout(10, TimeUnit.SECONDS);
    client.setReadTimeout(10, TimeUnit.SECONDS);
    //https?,??,???
    if (url.startsWith(StringUtil.URL_PREFIXs) && socketFactory != null) {
        client.setSslSocketFactory(socketFactory);
    }

    return client;
}