我正在使用WSDL編寫SOAP客戶端。我在PHP中有類似的代碼,工作正常。但是,我在Java中遇到了一些問題。這裏是我的代碼片段:使用WSDL的SOAP客戶端
我應該在哪裏定義我的本地WSDL文件的路徑?
任何想法是值得歡迎的。
2014年11月3日下午4時18分27秒 com.sun.xml.internal.messaging.saaj.soap:
try { SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); SOAPConnection soapConnection = soapConnectionFactory.createConnection(); SOAPMessage soapResponse = soapConnection.call(mySampleQuery(), "https://www.webpagename.com"); // Process the SOAP Response printSOAPResponse(soapResponse); soapConnection.close(); } catch (Exception e) { System.err.println("Error occurred while sending SOAP Request to Server"); e.printStackTrace(); } private static SOAPMessage queryFlightsByAerodrome() throws Exception { MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL); SOAPMessage soapMessage = messageFactory.createMessage(); SOAPPart soapPart = soapMessage.getSOAPPart(); String serverURI = "localfolder/wsdlfilename.wsdl"; // SOAP Envelope SOAPEnvelope envelope = soapPart.getEnvelope(); envelope.addNamespaceDeclaration("mydata", serverURI); // SOAP Body SOAPBody soapBody = envelope.getBody(); SOAPElement soapBodyElem = soapBody.addChildElement("mySampleRequest", "mydata"); SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("userId"); soapBodyElem1.addTextNode("testuser"); //...here I create soapBodyElem1 MimeHeaders headers = soapMessage.getMimeHeaders(); headers.addHeader("SOAPAction", serverURI + "queryTestData"); System.out.println("Content description: "+soapMessage.getContentDescription()); soapMessage.saveChanges(); /* Print the request message */ System.out.print("Request SOAP Message:"); soapMessage.writeTo(System.out); System.out.println(); return soapMessage; }
運行此代碼我碰到下面的錯誤在行
SOAPMessage soapResponse = soapConnection.call(mySampleQuery(),"https://www.webpagename.com")
後.MessageImpl identifyContentType SEVERE:SAAJ0537:無效的Content-Type。可能是 錯誤消息而不是SOAP消息發生錯誤 將SOAP請求發送到服務器 com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl:無效 Content-Type:text/html。這是一個錯誤消息,而不是SOAP 響應? at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(Unknown Source)at com.aslogic.isagent.MyAgent.main(MyAgent.java:46) 由com.sun引起.xml.internal.messaging.saaj.SOAPExceptionImpl: 無效的Content-Type:text/html。這是一條錯誤消息,而不是 SOAP響應?在在 com.sun.xml.internal.messaging.saaj.soap.MessageImpl.identifyContentType(未知 源)在 com.sun.xml.internal.messaging.saaj.soap.MessageFactoryImpl.createMessage(未知 源) com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(未知 來源)
你確定你的服務的URL是正確的一切嗎?用瀏覽器檢查什麼它返回 – JIV 2014-11-03 15:56:55
@JIV:正如我所說,我在PHP類似的代碼,它工作正常。所以,這意味着服務網址是正確的。但是,在PHP中,我定義了WSDL文件和證書文件的路徑。我不知道如何在Java中執行此操作。 – 2014-11-03 16:09:38
其實我不認爲你正確地建設的要求,也許你應該看看在WSDL2Java的,並嘗試調用它從WSDL生成客戶端存根,將是您更容易 – JIV 2014-11-03 16:24:22