Okhttp: OkHttpClient.setSslSocketFactory missing from OkHttp v3?

Created on 5 Jan 2016  路  5Comments  路  Source: square/okhttp

Hello,

Apologies in advance if I have missed it, but the method OkHttpClient.setSslSocketFactory appears to be missing from v3 of the API.

I am using this call to enable TLS on Android < 5:

OkHttpClient client = new OkHttpClient();
client.setSslSocketFactory(new TLSSocketFactory());

Where TLSSocketFactory is described here: http://blog.dev-area.net/2015/08/13/android-4-1-enable-tls-1-1-and-tls-1-2/

Is there an alternative way of doing this in v3?

Most helpful comment

Try the builder:

OkHttpClient client = new OkHttpClient.Builder()
    .sslSocketFactory(new TLSSocketFactory())
    .build();

All 5 comments

Try the builder:

OkHttpClient client = new OkHttpClient.Builder()
    .sslSocketFactory(new TLSSocketFactory())
    .build();

Awesome, thanks.

@swankjesse I have tried your example but that seems to be deprecated. I used the alternate sslsocketfactory method but it is'nt working for me.

  • sslSocketFactory(
    SSLSocketFactory sslSocketFactory, X509TrustManager trustManager)

I change the TLS version to 1.2 and for the trustManager, I used the system default trustManager. But the tls version which is sent during api call is the device version not the version I overwritten.

My sslSocketFactory method:

SLContext context = SSLContext.getInstance("TLS");
context.init(null, null, null);
sslSocketFactory = context.getSocketFactory();

Also TLS version I enabled.
((SSLSocket)socket).setEnabledProtocols(new String[] {"TLSv1.2"});

My trustManager method:

public static X509TrustManager getSystemDefaultTrustManager()
{
try {
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(
TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init((KeyStore) null);
TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {
throw new IllegalStateException("Unexpected default trust managers:"
+ Arrays.toString(trustManagers));
}
return (X509TrustManager) trustManagers[0];
} catch (GeneralSecurityException e) {
throw new AssertionError(); // The system has no TLS. Just give up.
}
}

Any help will be much appreciated, TIA.

@pavithra19 please post this sort of usage question to stack overflow. Or if you can provide a reproducible runnable test case, they raise a new issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

GuiForget picture GuiForget  路  3Comments

sargunv picture sargunv  路  3Comments

theotherp picture theotherp  路  3Comments

lyf571321556 picture lyf571321556  路  3Comments

vanshg picture vanshg  路  3Comments