how to set soap Header using ksoap2 - android

I need to set the soap header information as part of authentication of a web method. I'm using ksoap2 API to call .NET web service. Here is the soap header with request.

    <DTHeader xmlns="">
      <Username> string </Username>
      <Password> string </Password>
    <MyTestMethod xmlns="">
       <ID> string </ID>
       <TransID> guid </TransID>

Can you please provide the android code to set the soap header "DTHeader" and set "Username" and "Password".


I did that this way:

import org.kxml2.kdom.Element;

then while preparing envelope

soapEnvelope.headerOut = new Element[1];
soapEnvelope.headerOut[0] = buildAuthHeader();
// ...send request...


private Element buildAuthHeader() {
    Element h = new Element().createElement(NAMESPACE, "AuthHeader");
    Element username = new Element().createElement(NAMESPACE, "user");
    username.addChild(Node.TEXT, USERNAME);
    h.addChild(Node.ELEMENT, username);
    Element pass = new Element().createElement(NAMESPACE, "pass");
    pass.addChild(Node.TEXT, PASSWORD);
    h.addChild(Node.ELEMENT, pass);

    return h;

obviously, change strings as needed.

Spent 2 days trying to get this to work with java and .net and have finally got it working..

ArrayList<HeaderProperty> headerProperty = new ArrayList<HeaderProperty>();

headerProperty.add(new HeaderProperty("guid", "value..."));,envelope,headerProperty); 

tried the above example and would add a header section but wasn't compatible with .net.

My piece of code works but requires KSoap2 version ksoap2-android-assembly-2.5.7-jar-with-dependencies.jar from here: ksoap jar file location

right click and view raw file and click save as.

Thanks for all the help in the forums as pointed me in the right direction...

Need Your Help

What are the differences between local branch, local tracking branch, remote branch and remote tracking branch?

git version-control git-branch git-remote

I just started using Git and I got really confused between different branches. Can anyone help me to figure out what the following branch types are?

Getting selected text with CKEditor Plugin on IE

javascript internet-explorer plugins ckeditor

I have made a plugin for CKEditor, but it relies on the currently selected text.