我有一个要求,我需要做沟通与第三方网站服务执行某些任务。
首先,我需要向会话控制管理器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;
}
}`
1条答案
按热度按时间mm5n2pyu1#
创建自己的urlstreamhandler,以便设置urlconnection参数,例如连接超时、读取超时
api规范
URL
: