Can't get server certificate from site

谁说我不能喝 提交于 2019-12-10 11:15:37

问题


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(); 

I get an exception saying that "getServerCertificates cannot be resolved."

I don't think its necessary to use keystore, is it??


回答1:


I get an exception saying that getServerCertificates cannot be resolved..

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());
        }
    }
}

I get a result like:

Sun RSA public key, 1024 bits
  modulus: 13069990984429476578...
  public exponent: 65537
Sun RSA public key, 1024 bits
  modulus: 14179907349200548596...
  public exponent: 65537

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());


来源:https://stackoverflow.com/questions/10643032/cant-get-server-certificate-from-site

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!