HttpsURLConnection: getDefaultSSLSocketFactory()

 
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.security.cert.CertPath;
import java.security.cert.CertificateFactory;
import java.util.ArrayList;
import java.util.List;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;

public class Main {
  public static void main(String args[]) throws Exception {
    SSLSocketFactory factory = HttpsURLConnection.getDefaultSSLSocketFactory();
    SSLSocket socket = (SSLSocket) factory.createSocket("127.0.0.1", 9999);
    socket.startHandshake();
    SSLSession session = socket.getSession();
    java.security.cert.Certificate[] servercerts = session.getPeerCertificates();

    List mylist = new ArrayList();
    for (int i = 0; i < servercerts.length; i++) {
      mylist.add(servercerts[i]);
    }

    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    CertPath cp = cf.generateCertPath(mylist);

    FileOutputStream f = new FileOutputStream("CertPath.dat");
    ObjectOutputStream b = new ObjectOutputStream(f);
    b.writeObject(cp);

  }
}
  
Home 
  Java Book 
    Networking  

HttpURLConnection:
  1. HttpURLConnection
  2. HttpURLConnection.HTTP_OK
  3. HttpURLConnection: getContentLength()
  4. HttpURLConnection: getContentType()
  5. HttpURLConnection: getDate()
  6. HttpURLConnection: getExpiration()
  7. HttpURLConnection: getHeaderFields()
  8. HttpURLConnection: getInputStream()
  9. HttpURLConnection: getLastModified()
  10. HttpURLConnection: getRequestMethod
  11. HttpURLConnection: getResponseCode()
  12. HttpURLConnection: getResponseMessage()
  13. HttpURLConnection: setInstanceFollowRedirects(boolean followRedirects)
  14. HttpURLConnection: setRequestMethod(String method) throws ProtocolException
  15. HttpsURLConnection: getDefaultSSLSocketFactory()