Example usage for com.squareup.okhttp OkHttpClient setHostnameVerifier

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

Introduction

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

Prototype

public OkHttpClient setHostnameVerifier(HostnameVerifier hostnameVerifier) 

Source Link

Document

Sets the verifier used to confirm that response certificates apply to requested hostnames for HTTPS connections.

Usage

From source file:org.perfcake.reporting.destination.InfluxDbDestination.java

License:Apache License

@Override
public void open() {
    Arrays.stream(tags.split(",")).map(StringUtil::trim).forEach(tagsArray::add);

    try {//from ww  w  .  jav a 2 s  . co m
        if ((keyStore != null && !"".equals(keyStore)) || (trustStore != null && !"".equals(trustStore))) {
            sslFactory = SslSocketFactoryFactory.newSslSocketFactory(keyStore, keyStorePassword, trustStore,
                    trustStorePassword);
        }
    } catch (PerfCakeException e) {
        log.warn("Unable to initialize SSL socket factory: ", e);
    }

    try {
        if (sslFactory != null) {
            final OkHttpClient client = new OkHttpClient();
            client.setSslSocketFactory(sslFactory);
            client.setHostnameVerifier((hostname, session) -> true);
            influxDb = InfluxDBFactory.connect(serverUrl, userName, password, new OkClient(client));
        } else {
            influxDb = InfluxDBFactory.connect(serverUrl, userName, password);
        }
        if (createDatabase) {
            influxDb.createDatabase(database);
        }
        influxDb.enableBatch(100, 500, TimeUnit.MILLISECONDS);
    } catch (RuntimeException rte) {
        influxDb = null;
        log.error("Unable to connect to InfluxDb: ", rte);
    }
}

From source file:org.tecrash.crashreport.DropboxUploadingJob.java

License:Open Source License

private OkHttpClient getClient() {
    OkHttpClient client = new OkHttpClient();
    //        client.networkInterceptors().add(new GzipRequestInterceptor());
    client.setHostnameVerifier(new HostnameVerifier() {
        @Override//from   w ww.  j  a v  a 2 s .  c  o m
        public boolean verify(String s, SSLSession sslSession) {
            return true;
        }
    });
    try {
        SSLContext sslContext = SSLContext.getInstance("TLS");

        // set up a TrustManager that trusts everything
        sslContext.init(null, new TrustManager[] { new X509TrustManager() {
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }

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

            public void checkServerTrusted(X509Certificate[] certs, String authType) {
            }
        } }, new SecureRandom());

        SSLSocketFactory sf = sslContext.getSocketFactory();
        client.setSslSocketFactory(sf);
    } catch (NoSuchAlgorithmException e) {
    } catch (KeyManagementException e) {
    }
    return client;
}

From source file:org.tecrash.crashreport.UptimeJob.java

License:Open Source License

private OkHttpClient getClient() {
    OkHttpClient client = new OkHttpClient();
    client.setHostnameVerifier(new HostnameVerifier() {
        @Override/*from  w  w w.j a  v a2 s  . c  om*/
        public boolean verify(String s, SSLSession sslSession) {
            return true;
        }
    });
    try {
        SSLContext sslContext = SSLContext.getInstance("TLS");

        // set up a TrustManager that trusts everything
        sslContext.init(null, new TrustManager[] { new X509TrustManager() {
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }

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

            public void checkServerTrusted(X509Certificate[] certs, String authType) {
            }
        } }, new SecureRandom());

        SSLSocketFactory sf = sslContext.getSocketFactory();
        client.setSslSocketFactory(sf);
    } catch (NoSuchAlgorithmException e) {
    } catch (KeyManagementException e) {
    }
    return client;
}

From source file:org.whispersystems.signalservice.internal.push.PushServiceSocket.java

private Response getConnection(String urlFragment, String method, String body) throws PushNetworkException {
    try {/*  w w w  . j av  a  2 s.  co m*/
        Log.w(TAG, "Push service URL: " + serviceUrl);
        Log.w(TAG, "Opening URL: " + String.format("%s%s", serviceUrl, urlFragment));

        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, trustManagers, null);

        OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient.setSslSocketFactory(context.getSocketFactory());
        okHttpClient.setHostnameVerifier(new StrictHostnameVerifier());

        Request.Builder request = new Request.Builder();
        request.url(String.format("%s%s", serviceUrl, urlFragment));

        if (body != null) {
            request.method(method, RequestBody.create(MediaType.parse("application/json"), body));
        } else {
            request.method(method, null);
        }

        if (credentialsProvider.getPassword() != null) {
            request.addHeader("Authorization", getAuthorizationHeader());
        }

        if (userAgent != null) {
            request.addHeader("X-Signal-Agent", userAgent);
        }

        return okHttpClient.newCall(request.build()).execute();
    } catch (IOException e) {
        throw new PushNetworkException(e);
    } catch (NoSuchAlgorithmException | KeyManagementException e) {
        throw new AssertionError(e);
    }
}

From source file:syncthing.api.SyncthingApiLongpollModule.java

License:Open Source License

@Provides
@SessionScope//from  w  ww .  j av a  2s.c  o  m
@Named("longpoll")
public SyncthingApi provideSyncthingApi(Gson gson, OkHttpClient okClient, SyncthingApiConfig config) {
    OkHttpClient client = okClient.clone();
    client.setConnectTimeout(LONGPOLL_CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    client.setReadTimeout(LONGPOLL_READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    client.setSslSocketFactory(SyncthingSSLSocketFactory.create(config.getCACert()));
    client.setHostnameVerifier(new NullHostNameVerifier());
    client.interceptors().add(new SyncthingApiInterceptor(config));
    Retrofit.Builder b = new Retrofit.Builder().addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create(gson)).client(client).baseUrl(config.getBaseUrl());
    if (BuildConfig.DEBUG) {
        b.validateEagerly();
    }
    return b.build().create(SyncthingApi.class);
}

From source file:syncthing.api.SyncthingApiModule.java

License:Open Source License

@Provides
@SessionScope//ww w  .  j  a va 2s  .co  m
public SyncthingApi provideSyncthingApi(Gson gson, OkHttpClient okClient, SyncthingApiConfig config) {
    OkHttpClient client = okClient.clone();
    client.setConnectTimeout(CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    client.setReadTimeout(READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    client.setSslSocketFactory(SyncthingSSLSocketFactory.create(config.getCACert()));
    client.setHostnameVerifier(new NullHostNameVerifier());
    client.interceptors().add(new SyncthingApiInterceptor(config));
    Retrofit.Builder b = new Retrofit.Builder().addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create(gson)).client(client).baseUrl(config.getBaseUrl());
    if (BuildConfig.DEBUG) {
        b.validateEagerly();
    }
    return b.build().create(SyncthingApi.class);
}

From source file:themeable.images.HttpsTrustManager.java

License:Apache License

public static OkHttpClient getTrustAllHttpClient() {
    try {/*from w ww.  ja va2 s .  c  o m*/
        // 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);
    }
}