我需要从HTTPS url中检索内容,但我总是收到这个错误:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
这是我的代码:
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
con.setHostnameVerifier(new AlwaysTrustHostnameVerifier());
class AlwaysTrustHostnameVerifier implements X509TrustManager, HostnameVerifier
{
@Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1)
throws CertificateException {
// TODO Auto-generated method stub
}
@Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1)
throws CertificateException {
// TODO Auto-generated method stub
}
@Override
public X509Certificate[] getAcceptedIssuers() {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean verify(String arg0, SSLSession arg1) {
// TODO Auto-generated method stub
return false;
}
}
所以我不知道我是否可以在不安装安全证书的情况下检索或发布到https url?我曾经使用过NodeJS/PHP,都是可行的,只是通过添加一些头信息来忽略错误,比如rejectUnauthorized为false。。。
Java中有没有任何变通方法可以让我在不安装安全证书的情况下向https发送请求?
感谢
这是openJDK中的一个错误,您可能需要获得openJDK的最新修复版本或迁移到OracleJDK。