У меня проблемы с размещением вызова SOAP API.Я получаю сообщение 400 Bad Request.Я не уверен, почему я получаю этот плохой ответ.Я слежу за документацией API, полученной непосредственно компанией-разработчиком программного обеспечения.
Мой тестовый класс:
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeader;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPConstants;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
public class TestClass {
public static void main(String args[]) {
String soapEndpointUrl = "https://telehealth.foracare.com/WebService/WS_DataInterchangeService.asmx";
String soapAction = "http://www.tdcare.com/DataInterchange";
callSoapWebService(soapEndpointUrl, soapAction);
}
private static SOAPMessage createSoapEnvelope(SOAPMessage soapMessage) throws SOAPException {
SOAPPart soapPart = soapMessage.getSOAPPart();
String targetNamespace = "http://www.tdcare.com/";
String xsi = "xsi";
String xsiURI = "http://www.w3.org/2001/XMLSchema-instance";
String xsd = "xsd";
String xsdURI = "http://www.w3.org/2001/XMLSchema";
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration(xsi, xsiURI);
envelope.addNamespaceDeclaration(xsd, xsdURI);
//SOAP Header
SOAPHeader soapHead = envelope.getHeader();
QName qname = new QName(targetNamespace, "sValidationSoapHeader", new String());
SOAPElement soapHeadElem = soapHead.addChildElement(qname);
SOAPElement soapHeadElem1 = soapHeadElem.addChildElement("Username");
soapHeadElem1.addTextNode("****");
SOAPElement soapHeadElem2 = soapHeadElem.addChildElement("Password");
soapHeadElem2.addTextNode("*****");
// SOAP Body
SOAPBody soapBody = envelope.getBody();
QName dataInterchangeQName = new QName(targetNamespace, "DataInterchange", new String());
SOAPElement dataInterchange = soapBody.addChildElement(dataInterchangeQName);
SOAPElement iCMDType = dataInterchange.addChildElement("iCMDType");
iCMDType.addTextNode("Q0001");
SOAPElement iData = dataInterchange.addChildElement("iData");
SOAPElement queryData = iData.addChildElement("QueryData");
SOAPElement account = queryData.addChildElement("Account");
account.addTextNode("*****");
SOAPElement password = queryData.addChildElement("Password");
password.addTextNode("******");
SOAPElement qSDate = queryData.addChildElement("QSDate");
qSDate.addTextNode("2010/01/01");
SOAPElement qEDate = queryData.addChildElement("QEDate");
qEDate.addTextNode("2010/02/01");
SOAPElement qMType = queryData.addChildElement("QMType");
qMType.addTextNode("1");
SOAPElement qCase = queryData.addChildElement("QCase");
return soapMessage;
}
private static void callSoapWebService(String soapEndpointUrl, String soapAction) {
try {
// Create SOAP Connection
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
// Send SOAP Message to SOAP Server
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(soapAction), soapEndpointUrl);
// Print the SOAP Response
System.out.println("Response SOAP Message:");
soapResponse.writeTo(System.out);
System.out.println();
soapConnection.close();
} catch (Exception e) {
System.err.println("\nError occurred while sending SOAP Request to Server!\nMake sure you have the correct endpoint URL and SOAPAction!\n");
e.printStackTrace();
}
}
private static SOAPMessage createSOAPRequest(String soapAction) throws Exception {
MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
SOAPMessage soapMessage = messageFactory.createMessage();
soapMessage = createSoapEnvelope(soapMessage);
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("Host", "telehealth.foracare.com");
headers.addHeader("SOAPAction", soapAction);
headers.setHeader("Content-Type", "text/xml; charset=utf-8");
soapMessage.saveChanges();
Iterator i = headers.getAllHeaders();
while(i.hasNext()) {
MimeHeader header = (MimeHeader)i.next();
System.err.println(header.getName() + " : "+ header.getValue());
}
/* Print the request message, just for debugging purposes */
System.out.println("Request SOAP Message:");
soapMessage.writeTo(System.out);
System.out.println("\n");
return soapMessage;
}
}
Это пример XML-запроса, которому я пытаюсь следовать:
И это сообщение, которое создает мой код:
/*
Constructed SOAP Request Message:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SOAP-ENV:Header>
<sValidationSoapHeader xmlns="http://www.tdcare.com/">
<Username>*****</Username>
<Password>*****</Password>
</sValidationSoapHeader>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<DataInterchange xmlns="http://www.tdcare.com/">
<iCMDType>Q0001</iCMDType>
<iData>
<QueryData>
<Account>*****</Account>
<Password>*****</Password>
<QSDate>2010/01/01</QSDate>
<QEDate>2010/02/01</QEDate>
<QMType>1</QMType>
<QCase/>
</QueryData>
</iData>
</DataInterchange>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
*/
Ниже приведены сообщения о трассировке и ошибках:
Apr 25, 2018 12:06:51 AM com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection post
SEVERE: SAAJ0008: Bad Response; Bad Request
Error occurred while sending SOAP Request to Server!
Make sure you have the correct endpoint URL and SOAPAction!
com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Bad response: (400Bad Request
at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:149)
at com.hersa.foraclient.TestClass.callSoapWebService(TestClass.java:131)
at com.hersa.foraclient.TestClass.main(TestClass.java:39)
Caused by: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Bad response: (400Bad Request
at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:264)
at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:145)
... 2 more
CAUSE:
com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Bad response: (400Bad Request
at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:264)
at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:145)
at com.hersa.foraclient.TestClass.callSoapWebService(TestClass.java:131)
at com.hersa.foraclient.TestClass.main(TestClass.java:39)
CAUSE:
com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Bad response: (400Bad Request
at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:264)
at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:145)
at com.hersa.foraclient.TestClass.callSoapWebService(TestClass.java:131)
at com.hersa.foraclient.TestClass.main(TestClass.java:39)
Любая помощь приветствуется!