简体   繁体   中英

How do I read all X Headers in asp.net

I want to be able to identify a mobile device using some form of ID.

I read this answer: Get unique, static id from a device via web request about reading x headers in asp.net but when I list all the headers using the following code I don't get any useful x headers

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

On my andoid device the only X header I get is HTTP_X_WAP_PROFILE

If you want to read HTTP headers you are going about it the wrong way. ServerVariables only holds some information from the headers.

You should be looking at the Request.Headers property. The following code will list all headers from the HTTP Request. Presumable the "x" headers you refer to will be there..

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

To read the header values you can use the code below.
I thin by using user-agent from which you can get an idea about the browser.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM