对于HTTPS的请求我们一般都是采取忽略的策略,一般而言直接实现X509TrustManager接口,简单的以SSLContext获取SSLSocketFactory就可以的了,实测6.0以上还未遇到过什么奇葩的问题,6.0以下的,正常情况下也不会有什么问题,但最近偶然发现一客户提供的https地址请求居然报错了,抛出了javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException的错误,百思不得其解,搜罗百度才发现原来OKHttp在android4.4中存在这样的错误,特在此做个记录!
对于解决OkHttp请求部分的https在Android4.4报javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException的错误的方法,就是重写SSLSocketFactory,代码如下:
class SSLSocketFactoryCompat : SSLSocketFactory { private var defaultFactory: SSLSocketFactory? = null companion object{ var protocols: Array<String>? = null var cipherSuites:Array<String>? = null init { try { var socket = SSLSocketFactory.getDefault ().createSocket() as SSLSocket if (socket != null) { var protocols:LinkedList<String> = LinkedList() for (protocol in socket.supportedProtocols){ if (!protocol.toUpperCase().contains("SSL")){ protocols.add(protocol) } } SSLSocketFactoryCompat.protocols = protocols.toArray(arrayOfNulls(protocols.size)) /* set up reasonable cipher suites */ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {