放置 SOAP 时 400 错误请求 API 调用
400Bad Request When palcing SOAP API Call
我无法调用 SOAP API。我收到一条 400 Bad Request 消息。我不确定为什么我会收到这种糟糕的回应。我正在关注软件公司直接获得的 API 文档。
我的测试Class:
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)
感谢任何帮助!
如果有效,试试这个。
String endPoint = "URL";
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
String soapString = "Complete-SOAP String including Body/Header";
InputStream is = new ByteArrayInputStream(soapString.getBytes());
SOAPMessage request = MessageFactory.newInstance().createMessage(null, is);
MimeHeaders headers = request.getMimeHeaders();
headers.addHeader("SOAPAction", "http://www.tdcare.com/DataInterchange");
headers.setHeader("Content-Type", "application/xml");
request.saveChanges();
SOAPMessage soapResponse = soapConnection.call(request, endPoint);
System.out.println(soapResponse);
我生成了 WSDL url 提供的相应 classes。其中包括一个 "stub" class,我曾用它来拨打 API。
package com.hersa.foraclient;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPException;
import org.apache.axis.message.SOAPHeaderElement;
import com.tdcare.www.WS_DataInterchangeServiceLocator;
import com.tdcare.www.WS_DataInterchangeServiceSoap;
import com.tdcare.www.WS_DataInterchangeServiceSoapProxy;
import com.tdcare.www.WS_DataInterchangeServiceSoapStub;
public class Class3 {
public static void main(String args[]) {
String iData2 = "<QueryData>\r\n" +
" <Account>*****</Account>\r\n" +
" <Password>******</Password>\r\n" +
" <QSDate>2010/01/01</QSDate>\r\n" +
" <QEDate>2010/02/01</QEDate>\r\n" +
" <QMType>1</QMType>\r\n" +
" <QCase/>\r\n" +
" </QueryData>";
try {
WS_DataInterchangeServiceLocator locator = new WS_DataInterchangeServiceLocator();
WS_DataInterchangeServiceSoapProxy proxy = new WS_DataInterchangeServiceSoapProxy(locator.getWS_DataInterchangeServiceSoapAddress());
WS_DataInterchangeServiceSoap service = locator.getWS_DataInterchangeServiceSoap();
WS_DataInterchangeServiceSoapStub stub = new WS_DataInterchangeServiceSoapStub(new URL(locator.getWS_DataInterchangeServiceSoapAddress()), locator);
String targetNamespace = "http://www.tdcare.com/";
QName qname = new QName(targetNamespace, "sValidationSoapHeader", new String());
SOAPHeaderElement sValidationSoapHeader = new SOAPHeaderElement(qname);
SOAPElement userName = sValidationSoapHeader.addChildElement("Username");
userName.addTextNode("*****");
SOAPElement headerPassword = sValidationSoapHeader.addChildElement("Password");
headerPassword.addTextNode("*****");
System.err.println(sValidationSoapHeader.toString());
stub.setHeader(sValidationSoapHeader);
String resp = stub.dataInterchange("Q0001", iData2);
System.err.println(resp);
} catch (ServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SOAPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我无法调用 SOAP API。我收到一条 400 Bad Request 消息。我不确定为什么我会收到这种糟糕的回应。我正在关注软件公司直接获得的 API 文档。
我的测试Class:
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)
感谢任何帮助!
如果有效,试试这个。
String endPoint = "URL";
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
String soapString = "Complete-SOAP String including Body/Header";
InputStream is = new ByteArrayInputStream(soapString.getBytes());
SOAPMessage request = MessageFactory.newInstance().createMessage(null, is);
MimeHeaders headers = request.getMimeHeaders();
headers.addHeader("SOAPAction", "http://www.tdcare.com/DataInterchange");
headers.setHeader("Content-Type", "application/xml");
request.saveChanges();
SOAPMessage soapResponse = soapConnection.call(request, endPoint);
System.out.println(soapResponse);
我生成了 WSDL url 提供的相应 classes。其中包括一个 "stub" class,我曾用它来拨打 API。
package com.hersa.foraclient;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPException;
import org.apache.axis.message.SOAPHeaderElement;
import com.tdcare.www.WS_DataInterchangeServiceLocator;
import com.tdcare.www.WS_DataInterchangeServiceSoap;
import com.tdcare.www.WS_DataInterchangeServiceSoapProxy;
import com.tdcare.www.WS_DataInterchangeServiceSoapStub;
public class Class3 {
public static void main(String args[]) {
String iData2 = "<QueryData>\r\n" +
" <Account>*****</Account>\r\n" +
" <Password>******</Password>\r\n" +
" <QSDate>2010/01/01</QSDate>\r\n" +
" <QEDate>2010/02/01</QEDate>\r\n" +
" <QMType>1</QMType>\r\n" +
" <QCase/>\r\n" +
" </QueryData>";
try {
WS_DataInterchangeServiceLocator locator = new WS_DataInterchangeServiceLocator();
WS_DataInterchangeServiceSoapProxy proxy = new WS_DataInterchangeServiceSoapProxy(locator.getWS_DataInterchangeServiceSoapAddress());
WS_DataInterchangeServiceSoap service = locator.getWS_DataInterchangeServiceSoap();
WS_DataInterchangeServiceSoapStub stub = new WS_DataInterchangeServiceSoapStub(new URL(locator.getWS_DataInterchangeServiceSoapAddress()), locator);
String targetNamespace = "http://www.tdcare.com/";
QName qname = new QName(targetNamespace, "sValidationSoapHeader", new String());
SOAPHeaderElement sValidationSoapHeader = new SOAPHeaderElement(qname);
SOAPElement userName = sValidationSoapHeader.addChildElement("Username");
userName.addTextNode("*****");
SOAPElement headerPassword = sValidationSoapHeader.addChildElement("Password");
headerPassword.addTextNode("*****");
System.err.println(sValidationSoapHeader.toString());
stub.setHeader(sValidationSoapHeader);
String resp = stub.dataInterchange("Q0001", iData2);
System.err.println(resp);
} catch (ServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SOAPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}