Android OKHTTP发起请求提示:SSLException: Unable to parse TLS packet header
Posted QQ1657379258
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android OKHTTP发起请求提示:SSLException: Unable to parse TLS packet header相关的知识,希望对你有一定的参考价值。
今天切换环境进行调试,某一个接口突然出现报错:
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,运行——成功!
--—————————————————撒花庆祝———————————————————
以上是关于Android OKHTTP发起请求提示:SSLException: Unable to parse TLS packet header的主要内容,如果未能解决你的问题,请参考以下文章
Android OkHttp + Retrofit 取消请求的方法