Here is the souce code in my Java program: URL u = new URL("https://www.mycompany.com/cgi-bin/APerl.cgi"); HttpURLConnection connection = (HttpsURLConnection) u.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestMethod("POST"); OutputStream out = connection.getOutputStream(); wout = new OutputStreamWriter(out); wout.write("hello, this is a testmsg to HTTPS"); wout.flush(); wout.close();
It works as expected: 1. If I have a trusted certificate is placed in JRE/lib/security directory, then HTTPS goes through to my CGI program 2. If I don't have the trusted certificate, then the communication fails because of a SSLHandshakeException "No trusted certificate found".
However, I have a problem: if my Java program starts without the certificate in place so the first few HTTPS connection fail, if later a trusted certificate is added to the JRE/lib/security directory, the same SSLHandshakeException error still happens.
It turns out that with my current program the certificate has to be there PRIOR to the start of my Java problem.
But the desired behavior is that when a correct certificate is added to the correct security directory, the HTTPS communication starts to work.
Could someone please help me on how to "manually" re-init the SSL handshaking and/or other setup steps? I searched through HttpsURLConnection, URL, URLConnection classes but found no clue. Do I need to use other classes to perform this dynamic SSL authentication?