gpt4 book ai didi

java - 从 java 字符串生成 SOAP 消息

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

我写了一个方法,它从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:保存多部分消息时出错

我的错误在哪里?

最佳答案

这是因为您没有传递正确的协议(protocol)格式的消息。您的代码未指定要使用哪个 SOAP 协议(protocol),这意味着它为 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);

并且它有效。

关于java - 从 java 字符串生成 SOAP 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32646444/

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