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?
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.
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.
Thanks @yschimke I have shared the question in stack overflow
@swankjesse
Most helpful comment
Try the builder: