最后没有办法 只有 强制绕过这个检查了
1 |
import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import javax.net.ssl.X509TrustManager; public class TrustAnyTrustManager implements X509TrustManager { public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[] {}; } } |
1 |
import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLSession; public class TrustAnyHostnameVerifier implements HostnameVerifier { public boolean verify(String hostname, SSLSession session) { // 直接Pass return true; } } |
就是要检查的时候 让他直接返回true 通过验证就OK了
在建立连接的时候 还要设置一下这个管理器
1 |
SSLContext context = SSLContext.getInstance("SSL"); context.init(null,new TrustManager[] { new TrustAnyTrustManager() }, new SecureRandom()); // 获取URL 进行连接 URL url = new URL(urlString); connection = (HttpsURLConnection) url.openConnection(); connection.setSSLSocketFactory(context.getSocketFactory()); connection.setHostnameVerifier(new TrustAnyHostnameVerifier()); connection.connect(); |