我收到一个异常,说 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());