我无法从我的网站(其他网站)获得证书。我使用HttpsURLConnection
和方法getServerCertificates
尝试了一些解决方案,但没有解决问题。
URL httpsURL = new URL("https://www.google.com.br/");
HttpsURLConnection connection = (HttpsURLConnection)httpsURL.openConnection();
Certificate[] certs = connection.getServerCertificates();
我得到一个例外,说“getServerCertificates无法解析。”
我认为没必要使用密钥库,是吗?
答案 0 :(得分:1)
我得到一个例外,说无法解析getServerCertificates ..
这很奇怪。你确定你使用的是正确的课程吗?如果我运行此代码:
import java.io.IOException;
import java.net.URL;
import java.security.cert.Certificate;
import javax.net.ssl.HttpsURLConnection;
public class Test {
public static void main(String[] args) throws IOException {
URL httpsURL = new URL("https://www.google.com.br/");
HttpsURLConnection connection = (HttpsURLConnection) httpsURL.openConnection();
connection.connect();
Certificate[] certs = connection.getServerCertificates();
for (Certificate cer : certs) {
System.out.println(cer.getPublicKey());
}
}
}
我得到的结果如下:
Sun RSA public key, 1024 bits
modulus: 13069990984429476578...
public exponent: 65537
Sun RSA public key, 1024 bits
modulus: 14179907349200548596...
public exponent: 65537
验证您使用的SSL套接字工厂,可能是出了问题。将此添加到您的代码中,看看它的结果(例如,对我来说是com.sun.net.ssl.internal.ssl.SSLSocketFactoryImpl
):
System.out.println(connection.getSSLSocketFactory().getClass());
System.out.println(connection.getDefaultSSLSocketFactory().getClass());