我正在使用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