I established an https connection between the server and the client, where the client is a Java program and the server is a servlet. I used the following code to print the certificate data from the server.
URL url = new URL("https://localhost:8443/cert"); HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); connection.setSSLSocketFactory(sslsocketfactory); connection.setDoOutput(true); if(connection!=null){ Certificate[] certs = connection.getServerCertificates();// #1 System.out.println("Cert Type : " + certs[0].getType()); System.out.println("Cert Hash Code : " + certs[0].hashCode()); System.out.println("Cert Public Key Algorithm : " + certs[0].getPublicKey().getAlgorithm()); System.out.println("Cert Public Key Format : " + certs[0].getPublicKey().getFormat()); System.out.println("\n"); }
But I get the following exception.
java.lang.IllegalStateException: connection not yet open
I thought that a handshake should take place immediately after calling theurl.openconnection () method. What is the problem?
The exception is the line number '# 1' (see comments in the code above)
source share