首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >创建不带ns1、ns2、ns3命名空间的soap请求

创建不带ns1、ns2、ns3命名空间的soap请求
EN

Stack Overflow用户
提问于 2018-08-27 16:05:32
回答 1查看 3.2K关注 0票数 2

我正在实现一个web服务客户端,它的请求应该是这样的,它与soap-ui一起工作。

代码语言:javascript
复制
<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>

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

代码语言:javascript
复制
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);

这样我就可以得到下面的请求

代码语言:javascript
复制
<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名称空间。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-29 14:39:00

我认为您正在做一些不必要的名称空间添加和添加多个XML节点,在对代码进行简单修改后,应该能够添加您想要添加的头文件。

代码语言:javascript
复制
    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);

我希望它能帮上忙。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52035059

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档