2011-08-17 163 views
3

我需要通過HTTPS使用javax.xml.soap爲SOAP調用設置超時 但是,我不知道該怎麼做,必須有一個技巧來做到這一點但我找不到它。如何爲SOAP調用設置超時

SOAPMessage sm = null; 
SOAPMessage response = null; 

SOAPConnectionFactory sfc = SOAPConnectionFactory.newInstance(); 
SOAPConnection connection = sfc.createConnection(); 

MessageFactory mf = MessageFactory.newInstance(); 
sm = mf.createMessage(); 
... 
... 
URL url = new URL("https://server:XXXX/blablabla); 
response = connection.call(sm, url); 

我看到有人這樣做:

if (xxxSoapService instanceof Stub) 
      ((Stub) xxxSoapService).setTimeout(10000); 

xxxSoapService擴展java.rmi.Remote和存根上進口org.apache.axis.client.Stub;

有可能是我在那裏失蹤的東西。

回答

0

假設您已經在後臺線程中執行.call()。你可以讓一個定時器在另一個線程上觸發並終止加載線程。

或者,由於SOAPMessage包含所有數據,因此您可以使用HttpUrlConnection發送消息。

HttpUrlConnection connection = // initialize me! 
connection.setReadTimeout(TIMEOUT_VALUE); 
SOAPMessage sm = // initialize me! 

// more stuff for your message 

connection.connect(); 

sm.writeTo(connection.getOutputStream()); 

這應該工作,除非我誤解了writeTo()的行爲。

+0

我怎樣才能得到答案?像以前一樣,我在做SOAPMessage response = connection.call(sm,server);我需要檢查錯誤的響應 – user393381

+0

使用connection.getInputStream()。這應該給你答覆的主體。要獲取標題,有幾個方法:getHeaderField(),getHeaderFieldKey()。檢查HttpURLConnection文檔以獲取更多詳細信息。 – George