大佬教程收集整理的这篇文章主要介绍了在Android 4.4中启用TLS 1.2,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我到目前为止所做的:
private <S> S createservice(Class<S> serviceClass) { Retrofit retrofit = builder.client(getNewhttpClient()).build(); return retrofit.create(serviceClass); } private OkhttpClient getNewhttpClient() { OkhttpClient.builder clientBuilder = new OkhttpClient.builder() .connectTimeout(10,TimeUnit.SECONDS) .writeTimeout(10,TimeUnit.SECONDS) .readTimeout(0,TimeUnit.minutES); // Disable timeouts for read return enableTls12OnPreLollipop(clientBuilder).build(); } public static OkhttpClient.builder enableTls12OnPreLollipop(OkhttpClient.builder client) { if (Build.VERSION.SDK_INT == Build.VERSION_CODEs.KITKAT) { try { client.sslSocketFactory(new TLSSocketFactory()); ConnectionSpec cs = new ConnectionSpec.builder(ConnectionSpec.MODERN_TLS) .tlsVersions(TlsVersion.TLS_1_2) .build(); List<ConnectionSpec> specs = new ArrayList<>(); specs.add(cs); specs.add(ConnectionSpec.COMPATIBLE_TLS); specs.add(ConnectionSpec.CLEARTEXT); client.connectionSpecs(specs); } catch (Exception exC) { Log.e("OkhttpClientProvider","Error while enabling TLS 1.2",exc); } } return client; }
TLSSocketFactory CLASS
public class TLSSocketFactory extends SSLSocketFactory { private SSLSocketFactory delegate; public TLSSocketFactory() throws Keymanagementexception,NoSuchAlgorithmException { SSLContext context = SSLContext.geTinstance("TLS"); context.init(null,null,null); delegate = context.getSocketFactory(); } @Override public String[] getDefaultCipherSuites() { return delegate.getDefaultCipherSuites(); } @Override public String[] getSupportedCipherSuites() { return delegate.getSupportedCipherSuites(); } @Override public Socket createSocket(Socket s,String host,int port,Boolean autoClosE) throws IOException { return enableTLSOnSocket(delegate.createSocket(s,host,port,autoClosE)); } @Override public Socket createSocket(String host,int port) throws IOException,UnkNownHostException { return enableTLSOnSocket(delegate.createSocket(host,port)); } @Override public Socket createSocket(String host,InetAddress localHost,int localPort) throws IOException,localHost,localPort)); } @Override public Socket createSocket(InetAddress host,int port) throws IOException { return enableTLSOnSocket(delegate.createSocket(host,port)); } @Override public Socket createSocket(InetAddress address,InetAddress localAddress,int localPort) throws IOException { return enableTLSOnSocket(delegate.createSocket(address,localAddress,localPort)); } private Socket enableTLSOnSocket(Socket socket) { if(socket != null && (socket instanceof SSLSocket)) { ((SSLSocket)socket).setEnabledProtocols(new String[] {"TLSv1.1","TLSv1.2"}); } return socket; } }
我的错误是:javax.net.ssl.SSLProtocolException:SSL握手中止:ssl = 0xb829bae0:SSL库失败,通常是协议错误:14077410:SSL例程:SSL23_GET_SERVER_HelLO:sslv3警报握手失败(external / openssl / ssl / s23_clnt. c:741 0x8d9e3990:0x00000000)
它使用的是https://developers.google.com/android/reference/com/google/android/gms/security/ProviderInstaller,您需要在项目中使用Google Api.
您只需要在您的应用程序中调用:
@Override public void onCreate() { super.onCreate(); try { ProviderInstaller.installIfNeeded(this); } catch (GooglePlayservicesRepairableException | GooglePlayservicesnotAvailableException E) { e.printStackTrace(); } }
以上是大佬教程为你收集整理的在Android 4.4中启用TLS 1.2全部内容,希望文章能够帮你解决在Android 4.4中启用TLS 1.2所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。