-
Notifications
You must be signed in to change notification settings - Fork 9.2k
Closed
Description
OkHttpClient.Builder mBuilder=mBuilder = new OkHttpClient.Builder();
mBuilder.sslSocketFactory(createSSLSocketFactory());
mBuilder.hostnameVerifier(new TrustAllHostnameVerifier());
mBuilder.build();
/**
* 默认信任所有的证书
* TODO 最好加上证书认证,主流App都有自己的证书
*
* @return
*/
@SuppressLint("TrulyRandom")
private static SSLSocketFactory createSSLSocketFactory() {
SSLSocketFactory sSLSocketFactory = null;
try {
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, new TrustManager[]{new TrustAllManager()},
new SecureRandom());
sSLSocketFactory = sc.getSocketFactory();
} catch (Exception e) {
}
return sSLSocketFactory;
}
private static class TrustAllManager 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 null;
}
}
private static class TrustAllHostnameVerifier implements HostnameVerifier {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
}
this will cause NullPointException in 3.1.2,but it's ok in 3.0.1
Caused by: java.lang.NullPointerException: Attempt to get length of null array
at okhttp3.internal.tls.RealTrustRootIndex.<init>(RealTrustRootIndex.java:31)
at okhttp3.internal.Platform.trustRootIndex(Platform.java:97)
at okhttp3.internal.Platform$Android.trustRootIndex(Platform.java:271)
at okhttp3.OkHttpClient.<init>(OkHttpClient.java:189)
at okhttp3.OkHttpClient.<init>(OkHttpClient.java:60)
at okhttp3.OkHttpClient$Builder.build(OkHttpClient.java:718)
Activity
swankjesse commentedon Feb 23, 2016
Change your TrustAllManager to do this:
lizhangqu commentedon Feb 23, 2016
@swankjesse thank you
zhenlimeng commentedon Feb 4, 2017
@swankjesse 谢谢 thank you
ewu-tarun commentedon Jun 13, 2017
@swankjesse it's working thanks a lot
ps993390891 commentedon Jul 4, 2017
javax.net.ssl.SSLHandshakeException: Handshake failed
ewu-tarun commentedon Jul 4, 2017
@ps993390891 you can add connection specification for handshake.
zqHero commentedon Jan 26, 2018
thank you haha
liang99312 commentedon Jan 29, 2018
thank you
levelh commentedon Aug 13, 2019
nice
zhanzengyu commentedon Feb 29, 2020
Has the problem been solved?
yangjunjin commentedon May 18, 2021
bug bug 崩溃