16

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.

<soap:Header>
    <DTHeader xmlns="http://myServer.com/webservices/">
      <Username> string </Username>
      <Password> string </Password>
    </DTHeader>
</soap:Header>
<soap:Body>
    <MyTestMethod xmlns="http://myServer.com/webservices/">
       <ID> string </ID>
       <TransID> guid </TransID>
     </MyTestMethod>
</soap:Body>

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

Steve
  • 213,761
  • 22
  • 232
  • 286
user698555
  • 163
  • 1
  • 1
  • 7

2 Answers2

29

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

with

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.

bigstones
  • 15,087
  • 7
  • 65
  • 82
  • Thanks for the quick response. Now I'm able to add soap header as part of soap request. But have one other problem. As part of the Web Method call, I have two parameters to be passed 'CalID' (defined as 'String' as part of .NET Web Service) and 'SessionID' (defined as 'guid' as part of .NET Web Service). Further I need help on 1. how to create guid and pass parameter with 'guid' as type. – user698555 Apr 11 '11 at 12:02
  • @user698555: if `guid` is a primitive type you could just treat it as a string/integer/... as there's no real integration between .net and ksoap. If it's a complex type, you'll have to make a Java version of that type and make it implement `KvmSerializable` and `Marshal`, see some articles about that here: http://seesharpgears.blogspot.com/search/label/ksoap (please remember to accept the answer if the question was satisfied, thanks!) – bigstones Apr 11 '11 at 13:13
  • Thanks a lot. Everything worked fine with your valuable help. Please let me know how to accept the answer as I'm new to this newsgroup. – user698555 Apr 11 '11 at 20:22
  • @user698555: glad it helped. to accept just click the tick mark next to the question, thank you again! – bigstones Apr 11 '11 at 20:49
  • I have same problem but could get guid as type: 04-25 `15:03:55.203: W/System.err(16822): java.lang.RuntimeException: String expected 04-25 15:03:55.228: W/System.err(16822): at org.kxml2.kdom.Node.addChild(Node.java:64) 04-25 15:03:55.228: W/System.err(16822): at org.kxml2.kdom.Node.addChild(Node.java:73)` please provide some example. I have id,string and guid. please my problem is : [link] (http://stackoverflow.com/questions/23284640/how-to-get-custom-message-headers-of-wcf-service-in-android) – Horrorgoogle Apr 25 '14 at 09:10
  • what is "user" and "pass" In username and pass element..?is it something to related to Webservices Keyword..? –  Sep 02 '14 at 05:18
  • @bigstones I have same scenario i have to pass the xml having header but there is no DTHeader tag in it so what should i do to make header object ??? – Raheel Mateen May 09 '16 at 17:33
  • @bigstones i dont want this line in y code Element h = new Element().createElement(NAMESPACE, "AuthHeader"); – Raheel Mateen May 09 '16 at 17:34
  • I have header like this string string – Raheel Mateen May 09 '16 at 17:35
10

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

androidHttpTransport.call(soap_action,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...

takrl
  • 6,356
  • 3
  • 60
  • 69
Lee
  • 101
  • 1
  • 2