How to add client certificate using Java(Soap client)

73 Views Asked by At

I am trying to implement soap client using java getting below error

java.net.SocketException: Unexpected end of file from server

But using postman agent I'm able to hit the endpoints (added client certificate and host)

I added PFX certificate into key store and trust store not worked

public static void main(String[] args) throws Exception {
    KeyStore clientStore = KeyStore.getInstance("JKS");
    String xmlFile2Send = "C:\\Users\\santosh.velagala\\Downloads\\eurochange\\request.xml";
    FileInputStream fin = new FileInputStream(xmlFile2Send);
    ByteArrayOutputStream bout = new ByteArrayOutputStream();

    // Copy SOAP file to the open connection.
    copy(fin, bout);
    fin.close();

    byte[] b = bout.toByteArray();
    StringBuffer buf = new StringBuffer();
    String s = new String(b);
    b = s.getBytes();
    clientStore.load(new FileInputStream("PFX CERTIFICATE"), "PFX_PASSWORD".toCharArray());
    KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    kmf.init(clientStore, "PFX_PASSWORD".toCharArray());
    KeyManager[] kms = kmf.getKeyManagers();
    KeyStore trustStore = KeyStore.getInstance("JKS");
    trustStore.load(new FileInputStream("PFX CERTIFICATE"), "PFX PASSWORD".toCharArray());
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    tmf.init(trustStore);
    TrustManager[] tms = tmf.getTrustManagers();

    final SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(kms, tms, new SecureRandom());
    SSLContext.setDefault(sslContext);

    HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
    System.setProperty("https.proxyHost", "uat.accessesb.com");
    System.setProperty("https.proxyPort", "443");
    HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
    HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
    URL url = new URL("Service URL");
    HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
    con.setRequestMethod("POST");
    con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko");
    con.setConnectTimeout(10000);
    con.setSSLSocketFactory(sslContext.getSocketFactory());
    con.setRequestProperty("Content-type", "application/xml;charset=UTF-8");
    con.setRequestProperty("Content-Language", "en-US");
    con.setDoInput(true);
    con.setDoOutput(true);
    con.setRequestProperty("Accept", "text/xml");
    con.setRequestProperty("SOAPAction", "urn:checkCardAvailability");
    OutputStream os = con.getOutputStream();
    OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
    osw.write("SOAP XML");
    osw.flush();
    osw.flush();

    osw.close();
    os.close(); // don't forget to close the OutputStream

    con.connect();
    System.out.println(con.getAllowUserInteraction());
    System.out.println(con.getResponseCode());
}

public static void copy(InputStream in, OutputStream out)
        throws IOException {

    synchronized (in) {
        synchronized (out) {
            byte[] buffer = new byte[256];
            while (true) {
                int bytesRead = in.read(buffer);
                if (bytesRead == -1)
                    break;
                out.write(buffer, 0, bytesRead);
            }
        }
    }
}
0

There are 0 best solutions below