Advertisement
imjyb1008work

Detecting a mobile browser in ASP.NET

Feb 14th, 2017
1,680
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.39 KB | None | 0 0
  1. //Soource:
  2. //https://www.codeproject.com/Articles/34422/Detecting-a-mobile-browser-in-ASP-NET
  3. public static bool isMobileBrowser()
  4. {
  5.     //GETS THE CURRENT USER CONTEXT
  6.     HttpContext context = HttpContext.Current;
  7.  
  8.     //FIRST TRY BUILT IN ASP.NT CHECK
  9.     if (context.Request.Browser.IsMobileDevice)
  10.     {
  11.         return true;
  12.     }
  13.     //THEN TRY CHECKING FOR THE HTTP_X_WAP_PROFILE HEADER
  14.     if (context.Request.ServerVariables["HTTP_X_WAP_PROFILE"] != null)
  15.     {
  16.         return true;
  17.     }
  18.     //THEN TRY CHECKING THAT HTTP_ACCEPT EXISTS AND CONTAINS WAP
  19.     if (context.Request.ServerVariables["HTTP_ACCEPT"] != null &&
  20.         context.Request.ServerVariables["HTTP_ACCEPT"].ToLower().Contains("wap"))
  21.     {
  22.         return true;
  23.     }
  24.     //AND FINALLY CHECK THE HTTP_USER_AGENT
  25.     //HEADER VARIABLE FOR ANY ONE OF THE FOLLOWING
  26.     if (context.Request.ServerVariables["HTTP_USER_AGENT"] != null)
  27.     {
  28.         //Create a list of all mobile types
  29.         string[] mobiles =
  30.             new[]
  31.                 {
  32.                     "midp", "j2me", "avant", "docomo",
  33.                     "novarra", "palmos", "palmsource",
  34.                     "240x320", "opwv", "chtml",
  35.                     "pda", "windows ce", "mmp/",
  36.                     "blackberry", "mib/", "symbian",
  37.                     "wireless", "nokia", "hand", "mobi",
  38.                     "phone", "cdm", "up.b", "audio",
  39.                     "SIE-", "SEC-", "samsung", "HTC",
  40.                     "mot-", "mitsu", "sagem", "sony"
  41.                     , "alcatel", "lg", "eric", "vx",
  42.                     "NEC", "philips", "mmm", "xx",
  43.                     "panasonic", "sharp", "wap", "sch",
  44.                     "rover", "pocket", "benq", "java",
  45.                     "pt", "pg", "vox", "amoi",
  46.                     "bird", "compal", "kg", "voda",
  47.                     "sany", "kdd", "dbt", "sendo",
  48.                     "sgh", "gradi", "jb", "dddi",
  49.                     "moto", "iphone"
  50.                 };
  51.  
  52.         //Loop through each item in the list created above
  53.         //and check if the header contains that text
  54.         foreach (string s in mobiles)
  55.         {
  56.             if (context.Request.ServerVariables["HTTP_USER_AGENT"].
  57.                                                 ToLower().Contains(s.ToLower()))
  58.             {
  59.                 return true;
  60.             }
  61.         }
  62.     }
  63.  
  64.     return false;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement