2013-06-01 20 views
0

我想知道是否有可能一些自定義標題添加到每個Web服務調用,然後從Web服務方法來訪問這些自定義標題:添加自定義標題,網頁servies電話和網絡服務的方法讀它

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

回答

2

在客戶端,您可以使用XMLHttpRequest.setRequestHeader。比如添加自定義標題:

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

在服務器端:

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

} 
+0

許多感謝慶要 – sparrows81