The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.net.ssl  [4 examples]

e501. Retrieving the Certification Path of an SSL Server

This example implements a client that connects to an SSL server and retrieves the server's certificates.

See also e211 Adding a Certificate to a Key Store.

    try {
        // Create the client socket
        int port = 443;
        String hostname = "hostname";
        SSLSocketFactory factory = HttpsURLConnection.getDefaultSSLSocketFactory();
        SSLSocket socket = (SSLSocket)factory.createSocket(hostname, port);
    
        // Connect to the server
        socket.startHandshake();
    
        // Retrieve the server's certificate chain
        java.security.cert.Certificate[] serverCerts =
            socket.getSession().getPeerCertificates();
    
        // Close the socket
        socket.close();
    } catch (SSLPeerUnverifiedException e) {
    } catch (IOException e) {
    } catch (java.security.cert.CertificateEncodingException e) {
    }

 Related Examples
e499. Creating an SSL Client Socket
e500. Creating an SSL Server Socket
e502. Disabling Certificate Validation in an HTTPS Connection


© 2002 Addison-Wesley.