简体   繁体   中英

Creating soap request without ns1, ns2, ns3 namespaces

I am implementing a web service client and its request should be like this, It works with 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>

I need to add an Authentication header and my work so far is below ,

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);

This way I get below request

<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>

And my question is what should I do to get the working request? I need to remove the ns1 and ns2 namespaces, I guess.

I think you are doing some unneccesary namespace addition and adding multiple XML nodes, following simple modification to your code should be able to add header you want to add.

    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);

I hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM