gpt4 book ai didi

java - 发出 SOAP API 调用时出现 400 错误请求

转载 作者:太空宇宙 更新时间:2023-11-04 10:30:51 25 4
gpt4 key购买 nike

我在调用 SOAP API 时遇到问题。我收到 400 Bad Request 消息。我不确定为什么我会得到这种糟糕的 react 。我正在关注由软件公司直接获得的 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 请求:

enter image description here

这是我的代码构造的消息:

 /*
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);

关于java - 发出 SOAP API 调用时出现 400 错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50014514/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com