2017-01-03 13 views
0

特定のタスクを実行するために第三者のWebサービスと通信する必要があるという要件があります。JavaでWebサービス呼び出しの接続タイムアウトと要求タイムアウトを設定する方法

まずセッションIDを取得するために、セッションコントロールマネージャのWebサービスにログイン用のSOAPリクエストを送信する必要があります。 iは、セッション制御マネージャWebサービスにログアウトSOAPリクエストを送信する必要が最後にWebサービス2.

にタスクを実行するための実際のSOAPリクエストを送信する必要があり、そのセッションIDを使用して

また、私は接続ごとにタイムアウトを設定し、コールごとにタイムアウトを設定する必要があります。

  1. soap webserviceコールの接続タイムアウトと要求タイムアウトを設定するにはどうすればよいですか?
  2. webserviceを3回呼び出すので、毎回接続タイムアウト/要求タイムアウトを設定する必要がありますか?以下は を使用すると、たとえばURLConnectionのパラメータを設定することができるように、サンプルコード

`

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

は、あなた自身のための、URLStreamHandlerを作成しています接続タイムアウト、読み込みタイムアウト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); 
+0

。ここにコードの一部があります。 – shaiksha

+0

'SOAPConnectionFactory connectionFactory = SOAPConnectionFactory.newInstance(); \t \t SOAPConnection soapConnection = connectionFactory.createConnection(); \t \t URL loginEndPoint =新しいURL( "http://16.18.19.11:8989/test"); \t \t URLConnection urlConnection = loginEndPoint.openConnection(); \t \t urlConnection.setConnectTimeout(60000); \t \t urlConnection.setReadTimeout(60000); \t \t \t \t loginResp = soapConnection.call(soapMessage、loginEndPoint); ' – shaiksha

+0

あなたのコード内の "/ path/to/webservice"とは何ですか? – shaiksha

関連する問題