How to use same soap connection for sending multiple soap requests in java
<p>I have a requirement where i need to do communicate with a thirdparty webservices for performing certain tasks.</p>

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

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

<p>At the end i need to send logout soap request to the session control manager webservices.</p>

<p>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 {</p>

<pre><code>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) &amp;&amp; !resp.contains("Fault")){
        System.out.println("Received Valid Response" );
        return true ;

    }
    else{
        System.out.println("Fault found in Response");
        return false;
    }
}
</code></pre>

<p>}`</p>