从网络方法获取标题 [英] Get header from web method

查看:32
本文介绍了从网络方法获取标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从 jquery ajax 调用中获取标头属性.我在标题中发送了一个代码,所以我需要在 webmethods 中读取它:

How can I get the header properties from a jquery ajax call. I am sending a code in the header so I need to read it in the webmethods:

$.ajax({
    type: "POST",
    url: url,
    data: data,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: success,
    error: error,
    headers: {
        'aaaa': "code"
    }
});

推荐答案

在客户端(我假设您请求 webmethod 时使用 asmx),您可以使用 HttpContext.Current 来获取当前的 HttpContext.通过阅读请求,您可以获得标题.

On the client-side (I am assuming asmx as you requested the webmethod), you can use the HttpContext.Current to get the current HttpContext. By reading the Request, you can get the headers.

读取所有标题的示例是:

An example to read all the headers would be:

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

要阅读您的特定标题,您可以阅读

To read your specific header, you can read the

HttpContext.Current.Request.Headers['aaa']

这篇关于从网络方法获取标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆