java - 如何在java中为我的web服务调用设置连接超时和请求超时

标签 java web-services session soap

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

首先,我需要向 session 控制管理器 Web 服务发送登录 SOAP 请求以获取 session ID。

使用该 session ID,我需要将执行任务的实际 SOAP 请求发送到 Webservices 2。

最后我需要向 session 控制管理器网络服务发送注销soap请求。

我还需要为每个调用设置连接超时和请求超时。

  1. 如何设置 SOAP Webservice 调用的连接超时和请求超时?
  2. 由于我将调用网络服务 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;
}
}`

最佳答案

创建您自己的 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);

关于java - 如何在java中为我的web服务调用设置连接超时和请求超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41452264/

相关文章:

web-services - 为什么基于 SOAP 的 Web 服务不是 RESTful?

java - 删除时 JPA "org.apache.openjpa.persistence.ArgumentException"

java - 如何组织 Java 文本冒险的类和列表

使用 Web 服务访问数据库的 C# 桌面应用程序 > PHP 或 C# Webservice

asp.net - Application_AuthenticateRequest 总是在 Session_Start 之前吗?

php - session_regenerate_id() 在 IE11/Edge 中不起作用

php - PHP 7 中的 session

java - 2个java进程共享JVM吗

java - 使用BeanUtils.copyProperties从Mybatis创建的对象到Spring Bean,Spring bean中的属性为null。为什么?

php - 如何通过Webservice PHP-MySQL获取base 64的镜像?