Java SSL communication with a secure URL and get response. You will need correct SSL certificate in keystore. Certificate should match by one in server you are going to communicate. You can get that certificate by exporting from browser or server etc and place in keystore i.e. JKS.

/**
 * 
 */
package com.test;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManagerFactory;

import org.apache.axis2.transport.http.CommonsHTTPTransportSender;
import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
import org.apache.commons.httpclient.protocol.SSLProtocolSocketFactory;

import com.sun.xml.messaging.saaj.client.p2p.HttpSOAPConnection;

/**
 * @author m.imran
 * 
 */
public class HttpsSoapClient {

  /**
   * @param args
   */
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    System.out.println("start");

    try {
      String host = "https://host name here";
      // int port = 9444;
      String certificateName = "keystore";
      String path = "D:/imran/" + certificateName + ".jks";
      char[] passphrase = "fsesdfsdf".toCharArray();
      SSLSocketFactory sslFactory = null;

      KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
      keyStore.load(new FileInputStream(path), passphrase);

      TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
      tmf.init(keyStore);
      SSLContext ctx = SSLContext.getInstance("SSL");
      ctx.init(null, tmf.getTrustManagers(), null);
      sslFactory = ctx.getSocketFactory();

      URL url = new URL(host);

      CommonsHTTPTransportSender commonsHTTPTransportSender = new CommonsHTTPTransportSender();
//      commonsHTTPTransportSender.
//      HttpSOAPConnection soapConnection = new HttpSOAPConnection();
//      HttpSOAPConnection  connw = (HttpSOAPConnection ) url.openConnection();
      HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
      conn.setSSLSocketFactory(sslFactory);
      conn.setRequestMethod("POST");
      // conn.setRequestProperty("Content-Length", data.length());

      System.out.println("****** Content of the URL ********");
      BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));

      String input;

      while ((input = br.readLine()) != null) {
        System.out.println(input);
        try {
          Thread.currentThread();
          Thread.sleep(6000L);
        }
        catch (InterruptedException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
      br.close();
      System.out.println("end.");

    }
    catch (KeyStoreException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    catch (NoSuchAlgorithmException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    catch (CertificateException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    catch (KeyManagementException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

  }

}

Tags: ,

Leave a Reply