1

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

Community
  • 1
  • 1
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143

2 Answers2

2

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
Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151
  • @Matt Wilko - I don't really know anything about Ultidev Web Server Pro. But you should be able to tell if the information you're looking for is there or not. Just try the code I provided. If you can't find the headers you're looking for in the Request.Headers collection then you don't have access to them. That collection contains all the HTTP Request headers. – Mike Dinescu Feb 18 '13 at 14:48
  • I tried this but the only x header I got on my android was the `x-wap-profile`. I guess this may be all I can get? – Matt Wilko Feb 18 '13 at 15:21
  • @Matt - you may wish to inspect the headers that your android phone submits with the request to see if there is a specific header that is being sent but stripped away by your server. The easiest way to do this might be to write your own TCP listener (server) and have it listen on port 80. Then, from your android phone, attempt to browse to your mock server and capture the request stream. That way you will see everything that is sent on the wire. – Mike Dinescu Feb 18 '13 at 15:58
1

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); 
        }       
    }
शेखर
  • 17,412
  • 13
  • 61
  • 117
  • thanks but I don't want to know the browser I want to find a unique device id which is why I was trying to find the x headers are listed on the page I linked to – Matt Wilko Feb 18 '13 at 15:37