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?
感謝您的建議。我對Java和使用API都很陌生。您能否讓我知道如何以HTML響應的形式接收郵件?目前,我正在接收Connection.call()的輸出作爲SOAPMessage對象類型。我應該改變什麼? – Aandal
當我使用curl命令和相同的請求XML時,我得到了下面的響應。下面的回答說它的肥皂,不是嗎? <皁:信封的xmlns:SOAP = 「http://schemas.xmlsoap.org/soap/envelope/」> 實施例 soap:Envelope> –
Aandal
您可以使用類似Wireshark的截取回應。 –