【问题标题】:SOAP client using WSDL使用 WSDL 的 SOAP 客户端
【发布时间】: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


【解决方案1】:

无效的内容类型。可能是错误消息而不是 SOAP 消息

这是你的第一条线索。

无效的内容类型:文本/html。这是错误消息而不是 SOAP 响应吗

这是另一个线索。把它们放在一起:

  1. 您的客户端正在连接到服务
  2. 该服务正在返回一个非 SOAP 响应负载(它正在返回 HTML),这就是 SAAJ 对此感到窒息的原因。

通常,Web 服务在响应标准 JavaEE 容器生成的错误页面时会返回 HTML,这与预期的 SOAP 响应相反。现在的问题是:返回的错误响应是什么?请与您的服务提供商联系以获取有关您做错了什么的反馈。另外,请考虑以某种方式记录所有影响您的客户的内容

相关

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-01
    • 1970-01-01
    • 2013-08-20
    • 1970-01-01
    • 2013-11-08
    • 1970-01-01
    相关资源
    最近更新 更多