2012-07-20 113 views
0

我有我的本地Apache Tomcat上運行的Web服務。我可以通過SoapUI成功地與之通話。但是,當我用Java編寫客戶端時,它不會給我一個迴應!製作到Web服務的調用通過Java客戶端

下面是客戶端代碼:

SOAPConnectionFactory myFct = SOAPConnectionFactory.newInstance(); 
    SOAPConnection myCon = myFct.createConnection(); 
    MessageFactory myMsgFct = MessageFactory.newInstance(); 
    SOAPMessage message = myMsgFct.createMessage(); 
    SOAPPart mySPart = message.getSOAPPart(); 
    SOAPEnvelope myEnvp = mySPart.getEnvelope(); 
    SOAPBody body = myEnvp.getBody(); 
    Name bodyName = myEnvp.createName("Ping", "ws","http://ws.myeclipseide.com/"); 
    SOAPBodyElement gltp = body.addBodyElement(bodyName); 
    Name myContent1 = myEnvp.createName("arg0"); 
    SOAPElement mySymbol1 = gltp.addChildElement(myContent1); 
    mySymbol1.addTextNode("test"); 
    message.saveChanges(); 

    URLEndpoint endPt = new URLEndpoint("http://localhost:8080/PingWebService/StringPingPort?WSDL"); 
    SOAPMessage reply = myCon.call(message, endPt); 
    myCon.close(); 
    System.out.println("Response: "+reply.getContentDescription()); 

通過soapUI的調用看起來是這樣的:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.myeclipseide.com/"> 
    <soapenv:Header/> 
    <soapenv:Body> 
     <ws:Ping> 
     <!--Optional:--> 
     <arg0>testing this</arg0> 
     </ws:Ping> 
    </soapenv:Body> 
</soapenv:Envelope> 

任何想法,爲什麼它不會通過Java工作???

回答

0

不工作

異常,錯誤消息,沒有電話,...?

乍一看,我什麼也看不到明顯的,但因爲你使用的是Eclipse,啓動TCP監視器的Eclipse下,通過運行你在Eclipse程序,檢查什麼是在線路上發送發出呼叫。

+0

「不工作」=>我得到響應消息爲 「零」。你看SYSOUT末它打印「迴應:空」,哦,順便說一句,我使用「MyEclipse的」和我似乎無法找到TCP/IP監控下的喜好! – tsure 2012-07-23 17:37:27

0

getContentDescription()「返回:描述此消息或空的內容,如果沒有說明已設置一個字符串」,而不是你的消息的內容。

試試這個:

ByteArrayOutputStream out = new ByteArrayOutputStream(); 
    reply.writeTo(out); 
    System.out.println("Response: "+out.toString());