1

我编写了从java字符串生成soap消息的方法:

private SOAPMessage createRequest(String msg) {
    SOAPMessage request = null;
    try {
        MessageFactory msgFactory = MessageFactory.newInstance();
        request = factory.createMessage();

        SOAPPart msgPart = request.getSOAPPart();
        SOAPEnvelope envelope = msgPart.getEnvelope();
        SOAPBody body = envelope.getBody();

        StreamSource _msg = new StreamSource(new StringReader(msg));
        msgPart.setContent(_msg);

        request.saveChanges();
    } catch(Exception ex) {
       ex.printStackTrace();
    }
}

然后,我尝试生成一些消息。例如:

createRequest("test message");

但是在这里-request.saveChanges();我发现了这个异常: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Error during saving a multipart message

我的错误在哪里?

4

1 回答 1

1

那是因为您没有传递正确的协议格式消息。您的代码没有指定您要使用的 SOAP 协议,这意味着它为 SOAP 1.1 消息创建了一个消息工厂。

因此,您需要传递正确的 SOAP1.1 消息。我复制了你的方法是这样的:

private static SOAPMessage createRequest(String msg) {
        SOAPMessage request = null;
        try {
            MessageFactory msgFactory = MessageFactory
                    .newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
            request = msgFactory.createMessage();

            SOAPPart msgPart = request.getSOAPPart();
            SOAPEnvelope envelope = msgPart.getEnvelope();
            SOAPBody body = envelope.getBody();

            javax.xml.transform.stream.StreamSource _msg = new javax.xml.transform.stream.StreamSource(
                    new java.io.StringReader(msg));
            msgPart.setContent(_msg);

            request.saveChanges();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return request;
    }

我用这个字符串调用它:

String soapMessageString = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Header/><SOAP-ENV:Body></SOAP-ENV:Body></SOAP-ENV:Envelope>";
createRequest(soapMessageString);

它有效。

于 2015-10-02T10:43:07.780 回答