OkHttp之javax.net.ssl.SSLHandshakeException: Handshake failed

最近在應(yīng)用OkHttp進(jìn)行網(wǎng)絡(luò)請(qǐng)求時(shí),由于服務(wù)器添加了對(duì)https的支持,所以客戶端也做了相應(yīng)的改變,結(jié)果,沒(méi)想到的是,麻煩出現(xiàn)了:

01-17 15:58:16.445 29679-29824/****** I/NetworkManagementSocketTagger: untagSocket(88)
01-17 15:58:16.450 29679-29679/****** W/System.err: javax.net.ssl.SSLHandshakeException: Handshake failed
01-17 15:58:16.525 29679-29679/****** W/System.err:     at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:390)
01-17 15:58:16.525 29679-29679/****** W/System.err:     at okhttp3.internal.connection.RealConnection.connectTls(RealConnection.kt:350)
01-17 15:58:16.525 29679-29679/****** W/System.err:     at okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.kt:308)
01-17 15:58:16.525 29679-29679/****** W/System.err:     at okhttp3.internal.connection.RealConnection.connect(RealConnection.kt:182)
01-17 15:58:16.525 29679-29679/****** W/System.err:     at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.kt:238)
01-17 15:58:16.525 29679-29679/****** W/System.err:     at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.kt:111)
01-17 15:58:16.525 29679-29679/****** W/System.err:     at okhttp3.internal.connection.ExchangeFinder.find(ExchangeFinder.kt:79)
01-17 15:58:16.525 29679-29679/****** W/System.err:     at okhttp3.internal.connection.Transmitter.newExchange$okhttp(Transmitter.kt:163)
01-17 15:58:16.525 29679-29679/****** W/System.err:     at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:35)
01-17 15:58:16.525 29679-29679/****** W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
01-17 15:58:16.525 29679-29679/****** W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87)
01-17 15:58:16.525 29679-29679/****** W/System.err:     at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:82)
01-17 15:58:16.525 29679-29679/****** W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
01-17 15:58:16.525 29679-29679/****** W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87)
01-17 15:58:16.525 29679-29679/****** W/System.err:     at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:84)
01-17 15:58:16.525 29679-29679/****** W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
01-17 15:58:16.525 29679-29679/****** W/System.err:     at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:71)
01-17 15:58:16.525 29679-29679/****** W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
01-17 15:58:16.525 29679-29679/****** W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87)
01-17 15:58:16.525 29679-29679/****** W/System.err:     at com.base.network.LoggerInterceptor.intercept(LoggerInterceptor.java:42)
01-17 15:58:16.525 29679-29679/****** W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
01-17 15:58:16.525 29679-29679/****** W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87)
01-17 15:58:16.525 29679-29679/****** W/System.err:     at com.base.network.HeaderInterceptor.intercept(HeaderInterceptor.java:44)
01-17 15:58:16.525 29679-29679/****** W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
01-17 15:58:16.525 29679-29679/****** W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87)
01-17 15:58:16.525 29679-29679/****** W/System.err:     at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.kt:194)
01-17 15:58:16.525 29679-29679/****** W/System.err:     at okhttp3.RealCall$AsyncCall.run(RealCall.kt:138)
01-17 15:58:16.525 29679-29679/****** W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
01-17 15:58:16.525 29679-29679/****** W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
01-17 15:58:16.525 29679-29679/****** W/System.err:     at java.lang.Thread.run(Thread.java:818)
01-17 15:58:16.525 29679-29679/****** W/System.err: Caused by: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0xb4ba4800: Failure in SSL library, usually a protocol error
01-17 15:58:16.525 29679-29679/****** W/System.err: error:14077102:SSL routines:SSL23_GET_SERVER_HELLO:unsupported protocol (external/openssl/ssl/s23_clnt.c:740 0xa9794f89:0x00000000)
01-17 15:58:16.525 29679-29679/****** W/System.err:     at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
01-17 15:58:16.525 29679-29679/****** W/System.err:     at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:318)
01-17 15:58:16.525 29679-29679/****** W/System.err:     ... 29 more

我在網(wǎng)上搜了很多資料,嘗試了網(wǎng)上說(shuō)的很多方法,有的說(shuō)是Android5.0以下報(bào)這個(gè)錯(cuò)誤,可是我拿Android9.0的華為手機(jī)測(cè)試,結(jié)果依然都是報(bào)這個(gè)錯(cuò)誤。
繼續(xù)在網(wǎng)上找呀找,找呀找,終于找到了一個(gè)靠譜的解決方法,在此深深的對(duì)簡(jiǎn)書(shū)網(wǎng)友(俗人浮生)表示感謝??????。
解決方法如下(上碼??):

        // Https 異常:javax.net.ssl.SSLHandshakeException: Handshake failed
        // Caused by: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x7a59e45208: Failure in SSL library, usually a protocol error
        // 解決在Android5.0版本以下https無(wú)法訪問(wèn)(親測(cè)5.0以上版本也報(bào)同樣的錯(cuò)誤,猜測(cè)應(yīng)該通過(guò)服務(wù)器配置協(xié)議兼容可以解決,目前是Android端自己做了兼容)
        ConnectionSpec spec = new ConnectionSpec.Builder(ConnectionSpec.COMPATIBLE_TLS)
                .tlsVersions(TlsVersion.TLS_1_2, TlsVersion.TLS_1_1, TlsVersion.TLS_1_0)
                .allEnabledCipherSuites()
                .build();
        // 兼容http接口
        ConnectionSpec spec1 = new ConnectionSpec.Builder(ConnectionSpec.CLEARTEXT).build();
        okHttpClientBuilder.connectionSpecs(Arrays.asList(spec, spec1));

最后附上參考簡(jiǎn)書(shū)網(wǎng)友俗人浮生的文章地址:
http://www.itdecent.cn/p/64499bdf6842
希望對(duì)遇到此類問(wèn)題的網(wǎng)友們有所幫助。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容