0

我必须用 SOAP 调用 Web 服务。我用 Java 制作了一个客户端,它产生以下 SOAPMessage:

<?xml version="1.0" encoding="utf-8" ?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:abc="http://company.com/SAMPLE/ABC">
    <SOAP-ENV:Header />
    <SOAP-ENV:Body>
        <abc:genXX>
            <ServiceRequestID>111</ServiceRequestID>
            <Code>328630962</Code>
        </abc:genXX>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

当我在应用程序中调用它时,该调用会在解析 SOAP 期间产生错误:

<SOAP:Header>
</SOAP:Header>
<SOAP:Body>
    <SOAP:Fault xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
        <faultcode>SOAP:Client</faultcode>
        <faultstring>Error during parsing of SOAP header</faultstring>
        <faultactor>http://sap.com/xi/XI/Message/30</faultactor>
        <detail>
            <SAP:Error SOAP:mustUnderstand="1"
                xmlns:SAP="http://sap.com/xi/XI/Message/30">
                <SAP:Category>XIProtocol</SAP:Category>
                <SAP:Code area="PARSER" />
                <SAP:P1 />
                <SAP:P2 />
                <SAP:P3 />
                <SAP:P4 />
                <SAP:AdditionalText />
                <SAP:Stack>System error in parser
                </SAP:Stack>
            </SAP:Error>
        </detail>
    </SOAP:Fault>
</SOAP:Body>
</SOAP:Envelope>

但是,当我使用具有 SOAP UI 的同一台计算机调用它时,Web 服务响应良好。我的应用程序中的客户端是这样制作的:

public void callWebservice(String serviceRequestID, String code) {
    try {
            // Create SOAP Connection
            SOAPConnectionFactory soapConnectionFactory =
               SOAPConnectionFactory.newInstance();
            SOAPConnection soapConnection = soapConnectionFactory.createConnection();

            // Send SOAP Message to SOAP Server
            URLEndpoint url = new URLEndpoint ("http://company.com:50000/");
            SOAPMessage soapResponse =  soapConnection.call(
               createSOAPRequest(serviceRequestID, code), url);


            {...}

            soapConnection.close();
        } catch (Exception e) {
           //
        }

private SOAPMessage createSOAPRequest(String serviceRequestID, 
    String code) throws SOAPException{
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage soapMessage = messageFactory.createMessage();

        SOAPPart soapPart = soapMessage.getSOAPPart();

        String serverURI = "http://company.com/SAMPLE/ABC";

        // SOAP Envelope
        SOAPEnvelope envelope = soapPart.getEnvelope();
        envelope.addNamespaceDeclaration("abc", serverURI);

        // SOAP Body
        SOAPBody soapBody = envelope.getBody();
        SOAPElement soapBodyElem = soapBody.addChildElement("genXXX", "abc");
        SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("ServiceRequestID");
        SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("Code");

        soapBodyElem1.addTextNode(serviceRequestID);
        soapBodyElem2.addTextNode(code);

        MimeHeaders headers = soapMessage.getMimeHeaders();
        headers.addHeader("SOAPAction", serverURI );

        String loginPassword = "USER:PASSWORD";
        headers.addHeader("Authorization", "Basic " + new  
          String(Base64.encodeBase64(loginPassword.getBytes())));

        soapMessage.saveChanges();

        return soapMessage;
    }

  }

我检查了标题和身份验证,这是正确的。如果我更改用户或密码,Webservice 会使用 401 Unauthorized 响应,所以我认为标头已按预期发送。

您对导致我的应用程序内部错误的原因有任何线索吗?

4

2 回答 2

0

聚会晚了几年,但因为我今天遇到了几乎相同的问题,所以解决方案如下:

为了按照 Richard L 的建议通过 SOAP 1.1 协议进行通信,请将“SOAPAction”标头更改为“http://sap.com/xi/WebService/soap1.1”

        MimeHeaders headers = soapMessage.getMimeHeaders();
        headers.addHeader("SOAPAction", "http://sap.com/xi/WebService/soap1.1");

这对我有用。

于 2021-07-07T15:17:34.470 回答
0

您正在调用 SAP PI(SAP 流程集成)中的 Web 服务。我是一名 PI 开发人员,可以在 SOAP 上使用一种称为 XI 协议的 SAP 专有协议,该协议在 SAP-SAP 场景中很有用。如果该服务应该由非 SAP 系统访问,它可能应该更改为常规 SOAP 1.1 协议。与 PI 团队交谈。

于 2015-10-06T14:44:28.420 回答