gpt4 book ai didi

java - 如何用java编写soap客户端?

转载 作者:行者123 更新时间:2023-11-30 08:18:48 27 4
gpt4 key购买 nike

我有 xml

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<SendRequest xmlns="http://tempuri.org/">
<auth xmlns="">
<Login xmlns="http://tempuri.org/">vcm</Login>
<Password xmlns="http://tempuri.org/">vcm</Password>
</auth>
<Request xmlns="">
<Request_code xmlns="http:// tempuri.org/">1</Request_code>
<Message_Code xmlns="http://tempuri.org/">1111</Message_Code>
<Params xmlns="http:// tempuri.org/">
<RequestParameter>
<Name />
<Value />
</RequestParameter>
</Params>
</Request>
</SendRequest>
</soap:Body>
</soap:Envelope>

和Java代码

MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();

// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.removeAttribute("xmlns:SOAP-ENV");
envelope.setPrefix("soap");
envelope.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema");
envelope.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance");

MimeHeaders mimeheaders = soapMessage.getMimeHeaders();
mimeheaders.addHeader("SOAPAction", "SendRequest");
SOAPHeader header = soapMessage.getSOAPHeader();
header.detachNode();

SOAPBody soapBody = envelope.getBody();
soapBody.setPrefix("soap");
SOAPElement sendRequest = soapBody.addChildElement("SendRequest");
SOAPElement auth = sendRequest.addChildElement("auth");
auth.addChildElement("Login")
.addTextNode("vcm");
auth.addChildElement("Password")
.addTextNode("vcm");

SOAPElement request = sendRequest.addChildElement("Request");
request.addChildElement("Request_code")
.addTextNode("1");
request.addChildElement("Message_Code")
.addTextNode("1111");
request.addChildElement("Params");
sendRequest.addAttribute(new QName("xmlns"), "http://tempuri.org/");
soapMessage.saveChanges();

/* Print the request message */
System.out.print("Request SOAP Message:");
soapMessage.writeTo(System.out);
System.out.println();

我控制台我看到标签没有“http://tempuri.org/ ”。例如,如果我正在写,

sendRequest.addAttribute(new QName("aaa"), "http://tempuri.org/") 

我有

<SendRequest aaa="http://tempuri.org/">

如何编写java代码?

感谢您的帮助!

最佳答案

您没有将命名空间添加到元素。

尝试

SOAPElement sendRequest = soapBody.addChildElement(
new QName("http://tempuri.org/", "SendRequest"));

而不是

SOAPElement sendRequest = soapBody.addChildElement("SendRequest");

请注意,将命名空间 URI 的多个引用分解为常量/变量可能是个好主意。

关于java - 如何用java编写soap客户端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29272841/

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