2
我使用JSOUP连接到很多 https网站。我使用的方法Jsoup.connect(URL),但它抛出异常:从google play旁路SSL警告
javax.net.ssl.SSLHandshakeException: Certificate not valid or trusted.
所以我用这个代码信任所有证书的SSL:
public static void enableSSLSocket() throws KeyManagementException, NoSuchAlgorithmException {
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) { }
public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) { }
}
};
// Install the all-trusting trust manager
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(
new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
return true;
}
});
}
catch (Exception e) {
//We can not recover from this exception.
e.printStackTrace();
}}
但SICE我使用代码上面有古尔警告在玩商店。
您的应用程序正在对Apache HTTP客户端使用X509TrustManager接口的不安全实现,从而导致安全漏洞。有关详细信息,请参阅此Google帮助中心文章,其中包括修复漏洞的最后期限。
如果我添加以下代码:
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
try {
chain[0].checkValidity();
} catch (Exception e) {
throw new CertificateException("Certificate not valid or trusted.");
}
}
警告了,但JSOUP不工作是不是有同样的异常再次合作。 那么有什么方法可以相信所有的SSL和绕过谷歌警告?提前致谢。
“那么有没有什么办法可以相信所有的SSL和绕过谷歌警告?” - 希望不是。 “但JSOUP不工作” - 那么也许你应该考虑打开一个单独的堆栈溢出问题,在那里提供你的JSoup问题的[mcve],并详细解释什么是“不工作”的含义。 – CommonsWare
@CommonWare我只是更新我的问题。希望你能帮我! – hoanngothanh
我建议你完全消除你的'enableSSLSocket()'方法。 – CommonsWare