3 Replies Latest reply: Oct 12, 2011 4:29 AM by rukbat RSS

    Open https connection in Java

    875275
      I am working in JDeveloper 10.1.3.

      I need to connect to https site. The remote site provides us URL and password protected client certification. I am able to connect this site JSP in the browser by presenting the client certificate. The remote https site validates the URL and send us data in XML format. I need to parse this on - fly xml and get Login URL to redirect user to the specified URL.


      so I tried to use httpsURLOpenconnection, but I always failes. Please give me some

      I tried openConnection in Java, but since the URL is HTTPS with client certifcation, it fails. I got "javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: No trusted certificate found"

      any idea? Please help. It is closing to the deadline.

      These are my classes
      ------------------------------------------------------------------------------------------

      public class SSO implements Serializable {

      private String entityNumber; // mid

      final String PORTAL_URL = "https:/mytest.com/mytest.aspx";
      public SSO() {
      }


      public void invokeRedirect() {
      //get the context object
      FacesContext context = FacesContext.getCurrentInstance();
      ExternalContext extContext = context.getExternalContext();
      Class factoryClass = null;
      // URLStreamHandlerFactory factory = null;
      String socksServer = "";
      String socksPort = "";

      if(context!=null){


      try{
      StringBuffer urlLink = new StringBuffer(PORTAL_URL);



      URL url=new URL(urlLink.toString());
      javax.net.ssl.HttpsURLConnection https = ( javax.net.ssl.HttpsURLConnection) url.openConnection();

      https.setSSLSocketFactory(getFactory(new File("C:\\java_home\\jre\\lib\\security\\mycert.jks"), "mypassword"));

      https.connect();

      }
      catch(Exception ex){
      ex.printStackTrace();
      }
      }

      }

      private SSLSocketFactory getFactory(File pKeyFile, String pKeyPassword ) throws Exception
      {
      KeyManagerFactory kmf= KeyManagerFactory.getInstance("SUNX509");
      TrustManagerFactory tmf= TrustManagerFactory.getInstance("SUNX509");
      KeyStore ksKeys = KeyStore.getInstance("JKS");
      KeyStore ksTrust = KeyStore.getInstance("JKS");

      InputStream keyInput = new FileInputStream(pKeyFile);
      ksKeys.load(keyInput, pKeyPassword.toCharArray());
      keyInput.close();

      String trustPass = "mypassword2";
      ksTrust.load(new FileInputStream("C:\\java_home\\jre\\lib\\security\\cacerts"),trustPass.toCharArray());

      kmf.init(ksKeys, pKeyPassword.toCharArray());
      tmf.init(ksTrust);



      SSLContext context = SSLContext.getInstance("TLS");
      context.init(kmf.getKeyManagers(), tmf.getTrustManagers(), new SecureRandom());

      return context.getSocketFactory();

      }