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+】

相关推荐
Smile灬凉城666几秒前
反序列化为啥可以利用加号绕过php正则匹配
开发语言·php
lsx20240611 分钟前
SQL MID()
开发语言
Dream_Snowar14 分钟前
速通Python 第四节——函数
开发语言·python·算法
西猫雷婶16 分钟前
python学opencv|读取图像(十四)BGR图像和HSV图像通道拆分
开发语言·python·opencv
鸿蒙自习室16 分钟前
鸿蒙UI开发——组件滤镜效果
开发语言·前端·javascript
星河梦瑾16 分钟前
SpringBoot相关漏洞学习资料
java·经验分享·spring boot·安全
黄名富20 分钟前
Redis 附加功能(二)— 自动过期、流水线与事务及Lua脚本
java·数据库·redis·lua
love静思冥想21 分钟前
JMeter 使用详解
java·jmeter
言、雲24 分钟前
从tryLock()源码来出发,解析Redisson的重试机制和看门狗机制
java·开发语言·数据库
TT哇31 分钟前
【数据结构练习题】链表与LinkedList
java·数据结构·链表