C# 从web方法获取标题
如何从jQueryAjax调用中获取头属性。我正在发送标题中的代码,因此我需要在webmethods中读取它:C# 从web方法获取标题,c#,webmethod,C#,Webmethod,如何从jQueryAjax调用中获取头属性。我正在发送标题中的代码,因此我需要在webmethods中读取它: $.ajax({ type: "POST", url: url, data: data, contentType: "application/json; charset=utf-8", dataType: "json", success: success, error: error, headers: {
$.ajax({
type: "POST",
url: url,
data: data,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: success,
error: error,
headers: {
'aaaa': "code"
}
});
$ajax只是XMLHttpRequest的包装器,因此您可以使用getAllResponseHeaders() 我的测试结果:
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json;charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?YzpcdcnNcYm9iX3VtZW50cdsf3x1x2aXNgc3R1ZGlvIDIwMTNcUHJvHNcVml0xwfgbYWxBUElcVml0YWxBUElcYXBpXENvb3JkaW5hdG9y?=
X-Powered-By: ASP.NET
Date: Wed, 21 Jan 2015 00:37:03 GMT
Content-Length: 21034
在客户端(我假设asmx是您请求的webmethod),您可以使用HttpContext.Current来获取当前的HttpContext。通过读取请求,您可以获得标题
读取所有标题的示例如下:
public string GetRequestHeaders()
{
HttpContext ctx = HttpContext.Current;
if (ctx?.Request?.Headers == null)
{
return string.Empty;
}
string headers = string.Empty;
foreach (string header in ctx.Request.Headers.AllKeys)
{
string[] values = ctx.Request.Headers.GetValues(header);
headers += string.Format("{0}: {1}", header, string.Join(",", values));
}
return headers;
}
要阅读特定标题,可以阅读
HttpContext.Current.Request.Headers['aaa']
HttpContext.Current.Request.Headers['aaa']