如何在java中为我的webservice调用设置连接超时和请求超时

时间:2017-01-03 20:59:32

标签: java web-services session soap

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

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

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

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

此外,我还需要为每次通话设置连接超时和请求超时。

  1. 我们如何为soap webservice调用设置连接超时和请求超时?
  2. 因为我将调用webservice 3次,我是否需要每次都设置连接超时/请求超时? 以下是示例代码
  3. `

    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;
    }
    }`
    

1 个答案:

答案 0 :(得分:1)

创建您自己的URLStreamHandler,以便您可以设置URLConnection参数,例如连接超时,读取超时

URL的API规范:

SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection();
// creating URL from string represtation
URL endpoint = new URL(null,
      "http://example.com/path/to/webservice",
      new URLStreamHandler() {
        @Override
        protected URLConnection openConnection(URL url) throws IOException {
          URL target = new URL(url.toString());
          URLConnection connection = target.openConnection();
          // Connection settings
          connection.setConnectTimeout(10000); // 10 sec
          connection.setReadTimeout(60000); // 1 min
          return(connection);
        }
      });

SOAPMessage result = connection.call(soapMessage, endpoint);