http失败:javax.net.ssl.sslhandshakeexception:链验证失败

0sgqnhkj  于 2021-06-26  发布在  Java
关注(0)|答案(1)|浏览(1312)

这个问题在这里已经有答案了

sslhandshakeexception-链验证失败,如何解决(6个答案)
android仿真器“链验证失败”连接开发者机器与自签名证书(6个答案)
三天前关门了。
我尝试用okhttp客户端调用api,但在android中没有成功。api在ios和postman上运行良好。
我得到一个 SSLHandshakeException 仅在android中。那么,原因是什么,如何解决呢?
okhttp库

implementation 'com.squareup.okhttp3:okhttp:3.9.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.9.0'

http客户端

HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder builder = new OkHttpClient().newBuilder();
if (BuildConfig.DEBUG)
    builder.addInterceptor(logging);
OkHttpClient client= builder.build();

日志:

javax.net.ssl.SSLHandshakeException: Chain validation failed
    at com.android.org.conscrypt.SSLUtils.toSSLHandshakeException(SSLUtils.java:362)
    at com.android.org.conscrypt.ConscryptEngine.convertException(ConscryptEngine.java:1134)
    at com.android.org.conscrypt.ConscryptEngine.readPlaintextData(ConscryptEngine.java:1089)
    at com.android.org.conscrypt.ConscryptEngine.unwrap(ConscryptEngine.java:876)
    at com.android.org.conscrypt.ConscryptEngine.unwrap(ConscryptEngine.java:747)
    at com.android.org.conscrypt.ConscryptEngine.unwrap(ConscryptEngine.java:712)
    at com.android.org.conscrypt.ConscryptEngineSocket$SSLInputStream.processDataFromSocket(ConscryptEngineSocket.java:849)
    at com.android.org.conscrypt.ConscryptEngineSocket$SSLInputStream.access$100(ConscryptEngineSocket.java:722)
    at com.android.org.conscrypt.ConscryptEngineSocket.doHandshake(ConscryptEngineSocket.java:238)
    at com.android.org.conscrypt.ConscryptEngineSocket.startHandshake(ConscryptEngineSocket.java:217)
    at okhttp3.internal.connection.RealConnection.connectTls(RealConnection.java:299)
    at okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.java:268)
    at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:160)
    at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:256)
    at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:134)
    at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:113)
    at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)

测试设备和操作系统版本
oneplus 6t(android 10)
三星s5(android 9)
三星a5(安卓6)
android仿真器(android 11)
lg nexus(安卓6)

hiz5n14c

hiz5n14c1#

这是一个复制的android模拟器“链验证失败”连接开发者机器与自签名证书和sslhandshakeexception-链验证失败,如何解决?
很可能是设备上的日期错误、证书过期(如果它在其他地方工作则不太可能)、或者android设备上缺少ca证书。
没有根案例,很难判断发生了什么。您可能只包含了一半的异常跟踪。我相信原因也在后面。

/**
     * Wraps the given exception if it's not already a {@link SSLHandshakeException}.
     */
    static SSLHandshakeException toSSLHandshakeException(Throwable e) {
        if (e instanceof SSLHandshakeException) {
            return (SSLHandshakeException) e;
        }

        return (SSLHandshakeException) new SSLHandshakeException(e.getMessage()).initCause(e);
    }

相关问题