问题描述
我无法从我的网站(无其他网站)取得凭证。我尝试了一些解决方案与 HttpsURLConnection
和方法 getServerCertificates
但没有解决问题。
I can't get the certificate from my website (neither other website). I tried some solutions with HttpsURLConnection
and the method getServerCertificates
but nothing solves the problem.
URL httpsURL = new URL("https://www.google.com.br/");
HttpsURLConnection connection = (HttpsURLConnection)httpsURL.openConnection();
Certificate[] certs = connection.getServerCertificates();
我得到一个异常说getServerCertificates无法解析。
I get an exception saying that "getServerCertificates cannot be resolved."
我认为不必使用密钥库,
I don't think its necessary to use keystore, is it??
推荐答案
这很奇怪。您确定使用正确的课程吗?如果我运行这个代码:
That's strange. Are you sure you are using the proper classes? If I run this code:
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
):
Verify what SSL socket factory you are using, maybe there is something wrong with that. Add this to you code and see what results from it (as an example, for me is com.sun.net.ssl.internal.ssl.SSLSocketFactoryImpl
):
System.out.println(connection.getSSLSocketFactory().getClass());
System.out.println(connection.getDefaultSSLSocketFactory().getClass());
这篇关于无法从站点获取服务器证书的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!