无法使用 OKHTTP3 在 IBM JRE8 上提取信任管理器

Unable to Extract trust manager on IBM JRE8 with OKHTTP3

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

我也在用OKHTTP3 version 4.8.1HTTP2 client。它在 Oracle JDK 8 上工作,但在 IBM JRE 8 上不工作。

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

谢谢

您依赖于长期弃用的方法

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();