如何使用相同的soap连接在java中发送多个soap请求

时间:2016-12-22 16:35:40

标签: java web-services soap soap-client

我有一个要求,我需要与第三方Web服务进行通信以执行某些任务。

首先,我需要向会话控制管理器webservice发送登录soap请求以获取会话ID。

使用该会话ID我需要将执行任务的实际soap请求发送到webservices 2。

最后,我需要将logout soap请求发送到会话控制管理器webservices。

我的问题是我需要创建一个soapConnection来单独发送每个soap请求吗?或者我可以使用一个soapconnection发送所有必需的肥皂请求?如果我可以为所有请求使用相同的soap连接,那么有人可以提供一个如何保留soap连接的小例子`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;
    }
}

}`

0 个答案:

没有答案