当前位置: 代码迷 >> 综合 >> OKHTTP sslsocketfactory 过期,解决方案
  详细解决方案

OKHTTP sslsocketfactory 过期,解决方案

热度:9   发布时间:2024-01-25 22:32:59.0

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

原因是单参数的 构造函数 被弃用。

            try {TrustManagerFactory trustManagerFactory = null;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 okHttpClient = new OkHttpClient.Builder().sslSocketFactory(sslSocketFactory, trustManager).build()}catch(Exception e){}

 

 

 

  相关解决方案