【问题标题】:Invalid content type error using SAAJ API in Java在 Java 中使用 SAAJ API 出现无效的内容类型错误
【发布时间】:2017-02-14 21:48:02
【问题描述】:

我正在尝试使用 SAAJ 访问 SAOP API。 我遵循了与创建 SOAP 请求正文的其他答案中相同的代码,并且 调用 SOAP API。 此特定服务不需要任何身份验证(如 API 文档中所述),也不需要任何输入参数。

From WSDL: (copied only relevant content)

<xs:complexType name="getCustomerNames">
<xs:sequence/>
</xs:complexType> 
...
<wsdl:operation name="getCustomerNames">
<wsdl:input message="tns:getCustomerNames" name="getCustomerNames"/>
<wsdl:output message="tns:getCustomerNamesResponse" name="getCustomerNamesResponse"/>
<wsdl:fault message="tns:SOAPException" name="SOAPException"/>
</wsdl:operation>

下面是创建soap主体并调用API的Java代码

SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();
SOAPConnection connection = scf.createConnection();
SOAPFactory sf = SOAPFactory.newInstance();
String serverURI = "com.webservices.services.authentication";
MessageFactory mf =  MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL); // I tried without parameters as well
SOAPMessage message = mf.createMessage();
message.getSOAPHeader().detachNode();           
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("auth", serverURI);
Name bodyName = sf.createName("getCustomerNames", "auth", serverURI);
SOAPBodyElement bodyElement = body.addBodyElement(bodyName);
URL endpoint = new URL(urlendpoint); // urlendpoint is the one that I took from WSDL file service endpoint.
SOAPMessage response = connection.call(message, endpoint);
connection.close();

每当我执行此操作时,我都会收到以下错误消息。我不明白为什么.. 相同的 url 返回 当我从 Unix 执行 curl 命令并发布由上述 java 代码创建的相同请求 xml 时的正确响应。

Feb 14, 2017 3:21:54 PM com.sun.xml.internal.messaging.saaj.soap.MessageImpl identifyContentType
SEVERE: SAAJ0537: Invalid Content-Type. Could be an error message instead of a SOAP message
com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Invalid Content-Type:text/html. Is this an error message instead of a SOAP response?

【问题讨论】:

    标签: java soap content-type soap-client saaj


    【解决方案1】:

    该错误意味着服务器使用 HTML 页面而不是 SOAP 响应进行响应。要对此进行调试,您需要拦截响应并查看 HTML 页面的内容;它可能包含有关该问题的更多信息。

    【讨论】:

    • 感谢您的建议。我对 Java 和使用 API 都很陌生。您能否让我知道如何以 HTML 响应的形式接收该消息。目前我正在接收 Connection.call() 作为 SOAPMessage 对象类型的输出。我应该把它改成什么?
    • 当我对相同的请求 XML 使用 curl 命令时,我得到以下响应。下面的回复说它的肥皂,不是吗? schemas.xmlsoap.org/soap/envelope"> 示例信封>
    • 你可以使用Wireshark之​​类的东西来拦截响应。
    • 不幸的是,我无权在我的系统上安装wireshark。但我尝试使用 chrome “postman” 应用扩展来发布消息。它像上面一样返回 XML 响应,甚至通过 curl 命令,我也能够与所有服务进行交互。我在之前的评论中发布的响应 xml 不是 SOAP 响应吗?请告诉我
    • 如果您没有安装Wireshark的权限,您可以使用其他工具:stackoverflow.com/questions/120022/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-25
    • 1970-01-01
    • 2020-04-17
    相关资源
    最近更新 更多