Javascript 将自定义头添加到web服务调用,并在web服务方法中读取它

Javascript 将自定义头添加到web服务调用,并在web服务方法中读取它,javascript,asp.net,web-services,soapheader,Javascript,Asp.net,Web Services,Soapheader,我想知道是否可以为每个web服务调用添加一些自定义头,然后从web服务方法访问这些自定义头: eg. soapclient.headers.add("test","valueoftest") and from web services: [WebMethod] public string helloworld() { return "Hello world" + getcustomheader } 我还需要在ajax调用中添加头,因此我需要知道在javascript中在何处添加这些自定义

我想知道是否可以为每个web服务调用添加一些自定义头,然后从web服务方法访问这些自定义头:

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

and from web services:

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

}
我还需要在ajax调用中添加头,因此我需要知道在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>";
var soapHeader=“[body]”;

在客户端,您可以使用
XMLHttpRequest.setRequestHeader
添加自定义头。例如:

XMLHttpRequest.setRequestHeader('Custom', 'MyHeader');
在服务器端:

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

}