sslSocketFactory not supported on JDK 9+

clientBuilder.sslSocketFactory(SSLSocketFactory) not supported on JDK 9+

at okhttp3.internal.platform.Jdk9Platform.trustManager(Jdk9Platform.kt:61)
at okhttp3.OkHttpClient$Builder.sslSocketFactory(OkHttpClient.kt:751)
at

1.升版本4.9.3以上

2、加个函数获取X509TrustManager

复制代码
public static X509TrustManager getX509TrustManager() {
        X509TrustManager trustManager = null;
        try {
            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));
            }
            trustManager = (X509TrustManager) trustManagers[0];
        } catch (Exception e) {
            e.printStackTrace();
        }

        return trustManager;
    }

3.创建的时候加上上面的X509TrustManager

复制代码
 private static synchronized OkHttpClient getInstance() {
        okHttpClient = new OkHttpClient().newBuilder()
                .connectTimeout(15, TimeUnit.SECONDS)
                .readTimeout(15, TimeUnit.SECONDS)
                .writeTimeout(15, TimeUnit.SECONDS)
                .sslSocketFactory(new SSLSocketClient().getSSLSocketFactory(),SSLSocketClient.getX509TrustManager())//配置
                .hostnameVerifier(new SSLSocketClient().getHostnameVerifier())//配置
                .build();
        if (okHttpClient == null) {
            okHttpClient = new OkHttpClient();
        }
        return okHttpClient;
    }

重点是下面这句

.sslSocketFactory(new SSLSocketClient().getSSLSocketFactory(),SSLSocketClient.getX509TrustManager())

参考链接:使用okhttp3,提示【clientBuilder.sslSocketFactory(SSLSocketFactory) not supported on JDK 9+】

相关推荐
徐小黑ACG31 分钟前
GO语言 使用protobuf
开发语言·后端·golang·protobuf
0白露2 小时前
Apifox Helper 与 Swagger3 区别
开发语言
Tanecious.3 小时前
机器视觉--python基础语法
开发语言·python
叠叠乐3 小时前
rust Send Sync 以及对象安全和对象不安全
开发语言·安全·rust
战族狼魂3 小时前
CSGO 皮肤交易平台后端 (Spring Boot) 代码结构与示例
java·spring boot·后端
Tttian6224 小时前
Python办公自动化(3)对Excel的操作
开发语言·python·excel
xyliiiiiL4 小时前
ZGC初步了解
java·jvm·算法
杉之5 小时前
常见前端GET请求以及对应的Spring后端接收接口写法
java·前端·后端·spring·vue
hycccccch5 小时前
Canal+RabbitMQ实现MySQL数据增量同步
java·数据库·后端·rabbitmq
独好紫罗兰5 小时前
洛谷题单2-P5713 【深基3.例5】洛谷团队系统-python-流程图重构
开发语言·python·算法