解析SOAP标头时出错

时间:2014-06-10 11:07:36

标签: java xml web-services soap

我必须用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的同一台计算机调用它时,webservice响应很好。我的应用程序中的客户端是这样做的:

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;
    }

  }

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

您对我的应用程序中导致错误的原因有任何疑问吗?

2 个答案:

答案 0 :(得分:0)

您正在SAP PI(SAP Process Integration)中调用Web服务。我作为PI开发人员工作,可以使用SAP专有协议而不是名为XI protocal的SOAP,这在SAP-SAP方案中非常有用。如果非SAP系统应该访问此服务,则应该将其更改为常规SOAP 1.1协议。与PI团队交流。

答案 1 :(得分: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");

这对我有用。