3

任何人都可以帮助解决这个问题。

我也在使用OKHTTP3 版本 4.8.1来编写HTTP2 客户端。它适用于 Oracle JDK 8,但不适用于 IBM JRE 8。

错误消息:java.lang.IllegalStateException:无法在 okhttp3.internal.Platform@e85a0ce8 上提取信任管理器,sslSocketFactory 是 com.ibm.jsse2.SSLSocketFactoryImpl 类。

谢谢

4

1 回答 1

4

您依赖于长期弃用的方法

https://github.com/square/okhttp/blob/cd722373281202492043f4294fccfe6f691ddc01/okhttp/src/main/kotlin/okhttp3/OkHttpClient.kt#L741

它已被弃用,因为它必须对 JVM 做出很多假设,这会在每次 JVM 更新或跨供应商时中断。您应该改为使用 X509TrustManager 作为参数调用该方法

https://github.com/square/okhttp/blob/cd722373281202492043f4294fccfe6f691ddc01/okhttp/src/main/kotlin/okhttp3/OkHttpClient.kt#L767

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));
}
X509TrustManager trustManager = (X509TrustManager) trustManagers[0];

SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[] { trustManager }, null);
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

OkHttpClient client = new OkHttpClient.Builder()
    .sslSocketFactory(sslSocketFactory, trustManager)
    .build();
于 2020-09-08T10:51:05.350 回答