【发布时间】:2014-12-30 07:15:29
【问题描述】:
我正在使用 WSDL 编写 SOAP 客户端。我在 PHP 中有类似的代码,效果很好。但是,我在 Java 中遇到了一些问题。这是我的代码 sn-p:
我应该在哪里定义本地 WSDL 文件的路径?
欢迎提出任何想法。
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") 行收到以下错误:
2014 年 11 月 3 日下午 4:18:27 com.sun.xml.internal.messaging.saaj.soap.MessageImpl identifyContentType 严重:SAAJ0537:无效的内容类型。可能 错误消息而不是 SOAP 消息 向服务器发送 SOAP 请求 com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl:无效 内容类型:文本/html。这是错误消息而不是 SOAP 回复?在 com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(未知 来源)在 com.aslogic.isagent.MyAgent.main(MyAgent.java:46) 引起:com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: 无效的内容类型:文本/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(未知 来源)
【问题讨论】:
-
您确定您的服务网址正确吗?检查浏览器返回的内容
-
@JIV:正如我所提到的,我在 PHP 中有类似的代码,并且运行良好。因此,这意味着服务 url 是正确的。但是,在 PHP 中,我定义了 WSDL 文件和证书文件的路径。我不知道如何在 Java 中做到这一点。
-
其实我不认为你正确构建请求,也许你应该看看 wsdl2java 并尝试调用从 wsdl 生成的客户端存根,这对你来说会容易得多
-
奇怪的是 System.out.println("Content description: "+soapMessage.getContentDescription());返回 NULL。这可能是问题吗?
标签: java web-services soap jax-ws