2017-02-14 195 views
0

我想使用SAAJ访问SAOP API。 我遵循了与创建SOAP请求正文的其他答案中存在的代码相同的代码,并且调用SOAP API的代码是 。 这个特定的服务不需要任何验证(如API文档中所述),也不需要任何输入参数。在Java中使用SAAJ 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

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(); 

我得到下面的错误消息,每当我执行该Java代码。我无法理解为什么..当我从Unix执行curl命令并发布由上面的java代码创建的相同请求xml时,相同的url返回 正确的响应。

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? 

回答

0

该错误表示服务器用HTML页面而不是SOAP响应进行响应。为了调试这个,你需要拦截响应并查看HTML页面的内容;它可能会包含更多关于该问题的信息。

+0

感谢您的建议。我对Java和使用API​​都很陌生。您能否让我知道如何以HTML响应的形式接收邮件?目前,我正在接收Connection.call()的输出作为SOAPMessage对象类型。我应该改变什么? – Aandal

+0

当我使用curl命令和相同的请求XML时,我得到了下面的响应。下面的回答说它的肥皂,不是吗? <皂:信封的xmlns:SOAP = “http://schemas.xmlsoap.org/soap/envelope/”> 实施例 Aandal

+0

您可以使用类似Wireshark的截取回应。 –