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

Add custom header to web servies call and read it in web services methods

本文关键字:服务 web 方法 并在 读取 调用 添加 自定义      更新时间:2023-09-26

我想知道是否有可能为每个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>";

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

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

服务器端:

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