Jefferson Fidencio
Jefferson Fidencio

Reputation: 381

Can't get server certificate from site

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??

Upvotes: 3

Views: 3433

Answers (1)

Bogdan
Bogdan

Reputation: 24580

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

Upvotes: 3

Related Questions