我如何读取asp.net中的所有X标头

时间:2013-02-18 14:37:06

标签: asp.net .net mobile http-headers ultidev

我希望能够使用某种形式的身份识别移动设备。

我读到了这个答案:Get unique, static id from a device via web request关于在asp.net中读取x标题但是当我使用以下代码列出所有标题时,我没有得到任何有用的x标题

    For Each var As String In Request.ServerVariables
        Response.Write("<b>" & var & "</b>: " & Request(var) & "<br>")
    Next

在我的andoid设备上,我得到的唯一X标头是HTTP_X_WAP_PROFILE

2 个答案:

答案 0 :(得分:2)

如果您想阅读HTTP标头,那么您将采用错误的方式。 ServerVariables仅保存标题中的部分信息。

您应该查看Request.Headers属性。以下代码将列出HTTP请求中的所有标头。推荐你所指的“x”标题就在那里..

For Each headerName As String In Request.Headers.AllKeys
    Response.Write("<b>" & headerName & "</b>: " & Request.Headers(headerName) & "<br>")
Next

答案 1 :(得分:1)

要阅读标题值,您可以使用以下代码 我使用user-agent来瘦,你可以从中了解浏览器。

<强> C#

var headers = Request.Headers.ToString();

// If you want it formated in some other way.
var headers = String.Empty;
foreach (var key in Request.Headers.AllKeys)
  headers += key + "=" + Request.Headers[key] + Environment.NewLine;

<强> VB.NET:

Dim headers = Request.Headers.ToString()

' If you want it formated in some other way.'
Dim headers As String = String.Empty
For Each key In Request.Headers.AllKeys
  headers &= key & "=" & Request.Headers(key) & Environment.NewLine
Next

来自Detecting mobile device user agents in ASP.NET (Android)

 //for Mobile device 
    protected override void OnInit(EventArgs e) 
    { 

        string userAgent = Request.UserAgent; 
        if (userAgent.Contains("BlackBerry") 
          || (userAgent.Contains("iPhone") || (userAgent.Contains("Android")))) 
        { 
            //add css ref to header from code behind 
            HtmlLink css = new HtmlLink(); 
            css.Href = ResolveClientUrl("~/mobile.css"); 
            css.Attributes["rel"] = "stylesheet";  
            css.Attributes["type"] = "text/css";  
            css.Attributes["media"] = "all";  
            Page.Header.Controls.Add(css); 
        }       
    }