سؤال

i want to know if it is possible to add some custom headers to each web services call and then access these custom header from web services methods:

eg. soapclient.headers.add("test","valueoftest")

and from web services:

[WebMethod]
public string helloworld()
{
return "Hello world" + getcustomheader

}

I need to add header also in ajax calls so i need to know where to add these custom header in javascript:

var soapHeader = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\"><soap12:Body>[body]</soap12:Body></soap12:Envelope>";
هل كانت مفيدة؟

المحلول

On client side, you can add custom header using XMLHttpRequest.setRequestHeader.For example:

XMLHttpRequest.setRequestHeader('Custom', 'MyHeader');

On server side:

[WebMethod]
public string helloworld()
{
  string customHeader = HttpContext.Current.Request.Header["Custom"];
  return "Hello world" + customHeader;

}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top