1

I am very new in SOAP with https .One of my projects requires making a SOAP call to access a web service over HTTPS.

the service provider only gave us three file .jks, .p12. and one code sample. .txt file my first question is it possible to call Webservice without WSDL file.?

and I wrote the following client code with the help of sample code which service provider gave me .

but I getting this error message : java.net.SocketException: Socket is not connected: connect

package javaapplication7;



import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.security.KeyStore;
import java.security.KeyStore.SecretKeyEntry;
import javax.crypto.SecretKey;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;

/**
 *
 * @author dhanish
 */
public class JavaApplication7 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

           try
        {
            String keyPath = "C:\\key.jks";
            String keyPass = "xxx";
            String keyType = "JKS";


            //path to SSL keystore

            System.setProperty("javax.net.ssl.keyStore", keyPath);
            System.setProperty("javax.net.ssl.keyStorePassword", keyPass);
            System.setProperty("javax.net.ssl.keyStoreType", keyType);

            //build the XML to post
            String xmlString = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:req=\"http://olp.sadad.com/sadadpaymentmanagement/service/olppaymentmanager/req\">";
            xmlString = xmlString + "\n" + "<soapenv:Header/><soapenv:Body>";
            xmlString = xmlString + "\n" + "<initiatePaymentDetailsReq>";
            xmlString = xmlString + "\n" + "<olpIdAlias>xxxx</olpIdAlias>";
            xmlString = xmlString + "\n" + "<merchantId>xxxx</merchantId>";
            xmlString = xmlString + "\n" + "<merchantRefNum>2016081870001</merchantRefNum>";
            xmlString = xmlString + "\n" + "<paymentAmount>100</paymentAmount>";
            xmlString = xmlString + "\n" + "<paymentCurrency>SAR</paymentCurrency>";
            xmlString = xmlString + "\n" + "<dynamicMerchantLandingURL>http://sweetroomksa.com/index.php?route=payment/custom/confirm</dynamicMerchantLandingURL>";
            xmlString = xmlString + "\n" + "<dynamicMerchantFailureURL>#</dynamicMerchantFailureURL>";
            xmlString = xmlString + "\n" + "</initiatePaymentDetailsReq>";
            xmlString = xmlString + "\n" + "</soapenv:Body></soapenv:Envelope>";

            //post XML over HTTPS
            URL url = new URL("https://xxx.xxx.com/soap?service=RB_OLP_INITIATE_PAYMENT"); //replace 
            HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoOutput( true );


            connection.setRequestProperty( "Content-Type", "text/xml"  );
            connection.setHostnameVerifier(new HostnameVerifier()
            {
                public boolean verify(String hostname, SSLSession session)
                {
                    return true;
                }
            });
            connection.connect();

            //tell the web server what we are sending
            OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
            writer.write(xmlString);
            writer.flush();
            writer.close();

            // reading the response
            InputStreamReader reader = new InputStreamReader(connection.getInputStream());
            StringBuilder buf = new StringBuilder();
            char[] cbuf = new char[ 2048 ];
            int num;
            while ( -1 != (num=reader.read( cbuf )))
            {
                buf.append( cbuf, 0, num );
            }
            String result = buf.toString();
            System.out.println(result);
        }
        catch(Exception e)
        {
            System.out.println(e.getCause());
            e.printStackTrace();
        }





    }

}

OUTPUT

null
java.net.SocketException: Socket is not connected: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:656)
    at sun.security.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:173)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:180)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
    at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:275)
    at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:371)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1104)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:998)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:153)
    at javaapplication7.JavaApplication7.main(JavaApplication7.java:78)

can anyone help me findout what is the error is.?

danish dani
  • 603
  • 2
  • 8
  • 16
  • Yes its possible to invoke web service without wsdl simply if we know endpoint URL.. Like you doing plain httpsURLConnection or SAAJ soap framework etc.. Try to connect with SOAP UI tool if you have ..Did you add certificate file in the java keystore ? Can you please specify complete error – Karthik Aug 23 '16 at 15:34
  • @Karthik Thank you for the information. i tryed with SOAPUI tool but result is same . getting error msg ** 2016-08-23 22:42:59 - Error getting response; org.apache.http.conn.HttpHostConnectException: Connection to https://b2brbtest.riyadbank.com refused** – danish dani Aug 23 '16 at 19:50
  • yes . I added certificate by using [ KeyStore Explorer 5.2.1](http://www.keystore-explorer.org/downloads.html) software . I open my .jks file in KeyStore Explorer software. then generate the certificate from .jks. then add to cacerts – danish dani Aug 23 '16 at 20:18
  • 1
    Try whether you are able to ping that server or telnet that server and port. There would be some restriction in firewall as well.[connection refused error](http://stackoverflow.com/questions/2333400/what-can-be-the-reasons-of-connection-refused-errors) – Karthik Aug 24 '16 at 07:22
  • @Karthik its not not ping . – danish dani Aug 24 '16 at 12:41

1 Answers1

2

i found what the error is and solved it. the reason of this error SSL port (default is 443) is not open on server's firewall. so i contact the service company .i gave my static public IP to them . gave me access permission

danish dani
  • 603
  • 2
  • 8
  • 16