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

相关推荐
MATLAB滤波16 分钟前
MATLAB中的绘图技巧
开发语言·matlab
天草二十六_简村人30 分钟前
spring-data-elasticsearch 3.2.4 实现桶bucket排序去重,实现指定字段的聚合搜索
java·spring boot·后端·spring·elasticsearch·架构·jenkins
致宏Rex1 小时前
Rust 入门指南(零):安装及 Cargo 管理器
开发语言·rust
伏虎山真人1 小时前
R门 - rust第一课陈天 -内存知识学习笔记
开发语言·r语言
好开心331 小时前
javaScript交互补充(元素的三大系列)
开发语言·前端·javascript·ecmascript
码农多耕地呗1 小时前
刷别的学校oj—河工大oj1073-1099
开发语言·c++·算法
未来的JAVA高级开发工程师2 小时前
网关登录校验
java·微服务
Envyᥫᩣ2 小时前
掌握C#中的异步编程:async和await关键字详解
开发语言·c#
sukalot2 小时前
windows C#-查询表达式基础(三)
开发语言·windows·c#