1

I have a requirement where i need to do communicate with a thirdparty webservices for performing certain tasks.

First i need to send login soap request to session control manager webservice to get the session ID.

using that session ID i need to send the actual soap request for performing the task to webservices 2.

At the end i need to send logout soap request to the session control manager webservices.

My question is do i need to create a soapConnection for sending each soap request separately? or can i use one soapconnection to send all the required soap requests? if i can use the same soap connection for all requests can someone provide a small example of how to retain the soap connection `public class Test {

public static void main(String args[]) throws Exception {


    SOAPMessage loginMessage = null;
    SOAPMessage operationMessage = null;
    SOAPMessage logoutMessage = null;

    SOAPMessage loginResp  = null;
    SOAPMessage operationResp  = null;
    SOAPMessage logoutResp  = null;
    String loginResponse = null;
    String logoutResponse = null;
    String operationResponse = null;

    SOAPConnectionFactory connectionFactory = SOAPConnectionFactory.newInstance();
    /**
     * Get SOAP connection.
     */
    SOAPConnection soapConnection = connectionFactory.createConnection();

    //Sending Login request

    loginResp=soapConnection.call(loginMessage, "https://IP:port/Login");

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    loginResp.writeTo(os);

    loginResponse = os.toString();

    System.out.println("The Login call has been made");
    System.out.println("The response message is : " + loginResponse);


    if (checkForValidResponse(loginResponse, "LoginResponse"))
    {
        System.out.println("The call is successful");

        operationResp = soapConnection.call(operationMessage, "https://IP:port/Login");

        os.reset();
        operationResp.writeTo(os);
        operationResponse = os.toString();

        System.out.println("The operation call has been made");
        System.out.println("The response message is : " + loginResponse);

        if(checkForValidResponse(operationResponse, "OperationResponse")){
            System.out.println("The operation call is successful");

        }else{
            System.out.println("The Operation Call was not successful");
        }

    }else{
        System.out.println("The Login Call was not successful");

    }

    logoutResp=soapConnection.call(logoutMessage, "https://IP:port/Login");

    os.reset();
    logoutResp.writeTo(os);

    logoutResponse = os.toString();

    System.out.println("The Logout call has been made");
    System.out.println("The response message is : " + logoutResponse);

    if (checkForValidResponse(logoutResponse, "LogoutResponse"))
    {
        System.out.println("The Logout call was successful");
    }else{
        System.out.println("The Logout call was Unsuccessful");
    }


    soapConnection.close();



}

private static Boolean checkForValidResponse(String resp, String responseRootNode) throws Exception {

    System.out.println("Expected Body Element:" +responseRootNode);

    if(resp.contains(responseRootNode) && !resp.contains("Fault")){
        System.out.println("Received Valid Response" );
        return true ;

    }
    else{
        System.out.println("Fault found in Response");
        return false;
    }
}

}`

shaiksha
  • 993
  • 5
  • 17
  • 35
  • Yes, you can re-use one instance of soapConnection for multiple response/requests Obtain SOAPConnectionFactory `SOAPConnectionFactory factory = SOAPConnectionFactory.newInstance();` Create SOAPConnection `SOAPConnection connection = factory.createConnection();` Connect and send request `SOAPMessage soapResponse = connection.call(requst, url);` – fg78nc Dec 22 '16 at 17:51
  • @fg78nc: Thank you so much. i need to use Pass the SessionId obtained from Login response in the next provisioning request. What datatype variable should i use to save this session id? – shaiksha Dec 23 '16 at 08:25
  • @fg78nc: As per your suggestion i have tried to write the code. can you check if i am going the right way? – shaiksha Dec 23 '16 at 13:14
  • I think you can pass sessionId as String in request header. – fg78nc Dec 23 '16 at 20:13
  • @fg78nc How to do timeout checks . say if i didnt receive the soap response within certain time, how to handle such scenarios – shaiksha Jan 03 '17 at 12:53
  • Please refer to http://stackoverflow.com/questions/9536616/setting-socket-read-timeout-with-javax-xml-soap-soapconnection – fg78nc Jan 04 '17 at 16:24

0 Answers0