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

zxlwwiss  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(444)

我有一个要求,我需要做沟通与第三方网站服务执行某些任务。
首先,我需要向会话控制管理器webservice发送登录soap请求以获取会话id。
使用该会话id,我需要将执行任务的实际soap请求发送到Webservices2。
最后,我需要向会话控制管理器webservices发送注销soap请求。
我还需要为每个调用设置连接超时和请求超时。
如何为soap webservice调用设置连接超时和请求超时?
由于我将调用webservice 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;
}
}`
mm5n2pyu

mm5n2pyu1#

创建自己的urlstreamhandler,以便设置urlconnection参数,例如连接超时、读取超时
api规范 URL :

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);

相关问题