当前位置: 首页 > 工具软件 > Packet.Net > 使用案例 >

Android OKHTTP发起请求提示:SSLException: Unable to parse TLS packet header

冉伯寅
2023-12-01

今天切换环境进行调试,某一个接口突然出现报错:

javax.net.ssl.SSLException: Unable to parse TLS packet header

at com.android.org.conscrypt.ConscryptEngine.unwrap(ConscryptEngine.java:807)

at com.android.org.conscrypt.ConscryptEngine.unwrap(ConscryptEngine.java:747)

at com.android.org.conscrypt.ConscryptEngine.unwrap(ConscryptEngine.java:712)

at com.android.org.conscrypt.ConscryptEngineSocket$SSLInputStream.processDataFromSocket(ConscryptEngineSocket.java:858)

看到SSLException,心里第一个想法:简单,肯定是HTTPS协议没有增加SSL校验。

private SSLSocketFactory createSSLSocketFactory() {
        SSLSocketFactory ssfFactory = null;
        try {
            SSLContext sc = SSLContext.getInstance("TLS");
            sc.init(null, new TrustManager[]{mMyTrustManager}, new SecureRandom());
            ssfFactory = sc.getSocketFactory();
        } catch (Exception ignored) {
            ignored.printStackTrace();
        }

        return ssfFactory;
    }
 //实现X509TrustManager接口
    public class MyTrustManager implements X509TrustManager {
        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }

        @Override
        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[0];
        }
    }

创建

new OkHttpClient.Builder()
                    .connectTimeout(50000, TimeUnit.MILLISECONDS)
                    .readTimeout(50000, TimeUnit.MILLISECONDS)
                    .writeTimeout(50000, TimeUnit.MILLISECONDS)
                    .sslSocketFactory(createSSLSocketFactory(), new MyTrustManager())
                    .addInterceptor(new SameRequestFilterInterceptor())
                    .retryOnConnectionFailure(true).build();

运行,还是报错Unable to parse TLS packet header。

见鬼了,切换到正式环境就没有问题,难道是后台的锅?【别管是不是,先把锅抛出去!】

灵光一闪,SSL是HTTPS引起的,询问后台新环境是HTTP还是HTTPS。得到结果是http。

果然是这样,将https,修改为http,运行——成功!

--—————————————————撒花庆祝———————————————————

 类似资料: