Skip to content
Advertisement

Sending custom header with ksoap2 to asp.net

So i’m trying to add some authentication variables to the header of my soap request. To authenticate the sender. The client is an android app and the server is using asp.net web forms.

The library i’m using on the client side is ksoap2. The problem i am having, is that when i resive my soap envelop on the server side, and check the header of the request, my custom header element is not there.

The only keys which are there are “Connection” “Content-Length” “Content-Type” “Accept-Encoding” “Host” “User-Agent” “SOAPAction” Where the name of the custom one is “AuthHeader” (The current code is from an answer, i took from the answer of another thread)

The Java code is written like this

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

envelope.headerOut = new Element[1];
envelope.headerOut[0] = h;

The webmethed is just this

[WebMethod]
public string ValidateRequest()
{
  Console.WriteLine(HttpContext.Current.Request.Headers.AllKeys);
  return "1.0";
}

Can any one tell me what i am doing wrong ?

Advertisement

Answer

Solved it. The problem is that the response’s header in asp.net’s webmethod, is not the header of the soap envelop.

To gain access to the soap envelop’s header, what you need to do it to add this to the webmethod class

public SoapUnknownHeader[] unknownHeaders;
[WebMethod]
[SoapHeader("unknownHeaders", Required = false)]
public string ValidateRequest()
{
  string value = unknownHeaders[0].Element["n0:user"].InnerText;
  Console.WriteLine(value);
  
  return "1.0";
}
/* Output: "USERNAME" */

The SoeapHeader tag under the WebMethod tag, will automatikly add the soap header to the array of headers “unknownHeaders”. Which then can be referenced in your code.

And why you need to add the n0: in front of the name, it do not know

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement