创建没有 ns1、ns2、ns3 命名空间的 SOAP 请求



我正在实现一个Web服务客户端,它的请求应该是这样的,它适用于soap-ui。

<soapenv:Envelope 
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:met="http://tempuri.org/">
<soapenv:Header>
<met:Authentication>
<met:Username>test</met:Username>
<met:Password>test</met:Password>
</met:Authentication>
</soapenv:Header>
<soapenv:Body>
<met:UpdateOrder>
<met:ID>5311221</met:ID>
<met:Status>true</met:Status>
</met:UpdateOrder>
</soapenv:Body>
</soapenv:Envelope>

我需要添加一个身份验证标头,到目前为止我的工作如下,

SOAPHeaderElement header=new SOAPHeaderElement("http://tempuri.org/","met");
header.setActor(null);
MessageElement usernameToken = new MessageElement(new QName("Authentication","met"));
header.addChild(usernameToken);
MessageElement userToken = new MessageElement(new QName("Username","met"));
userToken.addTextNode(userName);
usernameToken.addChild(userToken);
MessageElement passToken = new MessageElement(new QName("Password","met"));
passToken.addTextNode(password);

usernameToken.addChild(passToken);
_stub.setHeader(header);

这样我得到下面的要求

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<soapenv:Header>
<ns1:met soapenv:mustUnderstand="0" xmlns:ns1="http://tempuri.org/">
<ns2:met xmlns:ns2="Authentication">
<ns3:met xmlns:ns3="Username">test</ns3:met>
<ns4:met xmlns:ns4="Password">test</ns4:met>
</ns2:met>
</ns1:met>
</soapenv:Header>
<soapenv:Body>
<UpdateOrder xmlns="http://tempuri.org/">
<ID>4576175</ID>
<Status>true</Status>
</UpdateOrder>
</soapenv:Body>
</soapenv:Envelope>

我的问题是我应该怎么做才能获得工作请求?我想我需要删除ns1ns2命名空间。

我认为您正在执行一些不必要的命名空间添加并添加多个XML节点,在对代码进行简单修改后,应该能够添加要添加的标头。

SOAPHeaderElement header=new SOAPHeaderElement("http://tempuri.org/","Authentication");
//**set the prefix met, though not necessary, the parser will default it to ns1 or something**/
header.setPrefix("met");
/**Add the username Node**/
SOAPElement user=header.addChildElement("userName");
/**Add the userName text**/
user.addTextNode("MyName");
/**Add the password node**/
SOAPElement password=header.addChildElement("password");
/**Add the password text**/
password.addTextNode("myPass");
/** Print the header if you wish to**/
System.out.println(header);     
/**set the header to stub, that's all, I think, you may setActor and mustunderstand**/
_stub.setHeader(header);

我希望它有所帮助。

最新更新