Sergi Pasoevi
Sergi Pasoevi

Reputation: 2851

Ksoap2 android - pass simple soap header

I'm using ksoap2 to call a WCF service, and as I understand, this library lets you create the request with headers in different ways, but I can't find a way to create a request with the simplest header. What I want to achieve is to have this structure in the xml that will be sent:

<soap:Header>

    <authToken>tokenhere</authToken>

</soap:Header>

The closest I can get to it is

Element[] header = new Element[1];
header[0] = new Element().createElement(null, "authToken");


header[0].setAttribute(null, "authToken" ,authToken);
envelope.headerOut = header;

Which produces this:

<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:d="http://www.w3.org/2001/XMLSchema" 
xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" 
xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
<v:Header><authToken authToken="PD94bWwgdmVyc" /></v:Header>
<v:Body></v:Body>
</v:Envelope>

Upvotes: 1

Views: 1538

Answers (2)

Nikunjkumar Kapupara
Nikunjkumar Kapupara

Reputation: 356

This is working for me to give the Security Request Header in kSOAP library in Android

SOAP REQ. Header:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cif="http://www.mawarid.ae/linkedCardsSummary/CRM/CIF.xsd">
       <soapenv:Header>
      <wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
         <wsse:UsernameToken wsu:Id="UsernameToken-14CBAE357AC169AFA614664925178422">
            <wsse:Username>XXXXXXXXXXXX</wsse:Username>
            <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">XXXXXXXXXXXX</wsse:Password>
                </wsse:UsernameToken>
      </wsse:Security>
   </soapenv:Header>

Android Code:

public static Element buildAuthHeader() {
        Element headers[] = new Element[1];
        headers[0]= new Element().createElement("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Security");
        headers[0].setAttribute(null, "mustUnderstand", "1");
        Element security=headers[0];

        //user token
        Element usernametoken = new Element().createElement(security.getNamespace(), "UsernameToken");
        usernametoken.setAttribute("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd", "Id", "UsernameToken-14CBAE357AC169AFA614664925178422");

        //username
        Element username = new Element().createElement(security.getNamespace(), "Username");
        username.addChild(Node.TEXT, HttpConstant.REQ_HEADER_USERNAME);
        usernametoken.addChild(Node.ELEMENT,username);

        // password
        Element password = new Element().createElement(security.getNamespace(), "Password");
        password.setAttribute(null, "Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");
        password.addChild(Node.TEXT, HttpConstant.REQ_HEADER_PASSWORD);
        usernametoken.addChild(Node.ELEMENT,password);


        headers[0].addChild(Node.ELEMENT, usernametoken);

        return headers[0];
    }





SoapSerializationEnvelope sSerialaEnvelop = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        sSerialaEnvelop.dotNet = true;
        sSerialaEnvelop.headerOut = new Element[1];
        sSerialaEnvelop.headerOut[0] = buildAuthHeader(); //// add security request header
        sSerialaEnvelop.bodyOut = sObject;
        sSerialaEnvelop.setOutputSoapObject(sObject);

Upvotes: 2

robocik
robocik

Reputation: 121

Try this:

envelope.headerOut = new org.kxml2.kdom.Element[1];
org.kxml2.kdom.Element elem1 = new org.kxml2.kdom.Element().createElement("","authToken");
elem1.addChild(org.kxml2.kdom.Node.TEXT, authToken);
envelope.headerOut[0]=elem1;

Upvotes: 0

Related Questions