Запрос 400Bad при вызове SOAP API Call - PullRequest
0 голосов
/ 25 апреля 2018

У меня проблемы с размещением вызова 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-запроса, которому я пытаюсь следовать:

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)

Любая помощь приветствуется!

Ответы [ 2 ]

0 голосов
/ 26 апреля 2018

Я сгенерировал соответствующие классы, предоставленные URL-адресом WSDL.К ним относится класс «заглушки», который я использовал для вызова 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();
        }

    }

}
0 голосов
/ 26 апреля 2018

Попробуйте, если работает.

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