移动检查方法在某些浏览器上不起作用

发布于 2024-12-04 14:33:15 字数 2321 浏览 1 评论 0原文

我正在使用下面的方法来检查移动版本,但它并不总是适用于所有桌面浏览器,而且这与 IE 版本无关,因为有些机器有 IE9,它保留在我的桌面版本和其他一些带有 IE9 重定向的机器上到移动版本时,我应该总是从 PC 返回 false,并且某些 IE8 用户也发生同样的情况,

此方法有任何例外吗?

        public static bool IsMobile()
        {
            //GETS THE CURRENT USER CONTEXT
            HttpContext context = HttpContext.Current;

            //FIRST TRY BUILT IN ASP.NT CHECK
            if (context.Request.Browser.IsMobileDevice)
            {
                return true;
            }
            //THEN TRY CHECKING FOR THE HTTP_X_WAP_PROFILE HEADER
            if (context.Request.ServerVariables["HTTP_X_WAP_PROFILE"] != null)
            {
                return true;
            }
            //THEN TRY CHECKING THAT HTTP_ACCEPT EXISTS AND CONTAINS WAP
            if (context.Request.ServerVariables["HTTP_ACCEPT"] != null &&
            context.Request.ServerVariables["HTTP_ACCEPT"].ToLower().Contains("wap"))
            {
                return true;
            }
            //AND FINALLY CHECK THE HTTP_USER_AGENT 
            //HEADER VARIABLE FOR ANY ONE OF THE FOLLOWING
            if (context.Request.ServerVariables["HTTP_USER_AGENT"] != null)
            {
                //Create a list of all mobile types
                string[] mobiles = {
"midp", "j2me", "avant", "docomo", 
"novarra", "palmos", "palmsource", 
"240x320", "opwv", "chtml",
"pda", "windows ce", "mmp/", 
"blackberry", "mib/", "symbian", 
"wireless", "nokia", "hand", "mobi",
"phone", "cdm", "up.b", "audio", 
"SIE-", "SEC-", "samsung", "HTC", 
"mot-", "mitsu", "sagem", "sony"
, "alcatel", "lg", "eric", "vx", 
"NEC", "philips", "mmm", "xx", 
"panasonic", "sharp", "wap", "sch",
"rover", "pocket", "benq", "java", 
"pt", "pg", "vox", "amoi", 
"bird", "compal", "kg", "voda",
"sany", "kdd", "dbt", "sendo", 
"sgh", "gradi", "jb", "dddi", 
"moto", "iphone"
};

                //Loop through each item in the list created above 
                //and check if the header contains that text
                foreach (string s in mobiles)
                {
                    if (context.Request.ServerVariables["HTTP_USER_AGENT"].ToLower().Contains(s.ToLower()))
                    {
                        return true;
                    }
                }
            }

            return false;
        }

I am using the method below to check if mobile version but it doesn't always work on all desktop browsers and its not a matter of IE versions, coz some machines has IE9 and it stayed on my desktop version and some other machines with IE9 redirected to the mobile version when i should always return false from PC and the same happened with some IE8 users

Is there any exceptions with this method ?

        public static bool IsMobile()
        {
            //GETS THE CURRENT USER CONTEXT
            HttpContext context = HttpContext.Current;

            //FIRST TRY BUILT IN ASP.NT CHECK
            if (context.Request.Browser.IsMobileDevice)
            {
                return true;
            }
            //THEN TRY CHECKING FOR THE HTTP_X_WAP_PROFILE HEADER
            if (context.Request.ServerVariables["HTTP_X_WAP_PROFILE"] != null)
            {
                return true;
            }
            //THEN TRY CHECKING THAT HTTP_ACCEPT EXISTS AND CONTAINS WAP
            if (context.Request.ServerVariables["HTTP_ACCEPT"] != null &&
            context.Request.ServerVariables["HTTP_ACCEPT"].ToLower().Contains("wap"))
            {
                return true;
            }
            //AND FINALLY CHECK THE HTTP_USER_AGENT 
            //HEADER VARIABLE FOR ANY ONE OF THE FOLLOWING
            if (context.Request.ServerVariables["HTTP_USER_AGENT"] != null)
            {
                //Create a list of all mobile types
                string[] mobiles = {
"midp", "j2me", "avant", "docomo", 
"novarra", "palmos", "palmsource", 
"240x320", "opwv", "chtml",
"pda", "windows ce", "mmp/", 
"blackberry", "mib/", "symbian", 
"wireless", "nokia", "hand", "mobi",
"phone", "cdm", "up.b", "audio", 
"SIE-", "SEC-", "samsung", "HTC", 
"mot-", "mitsu", "sagem", "sony"
, "alcatel", "lg", "eric", "vx", 
"NEC", "philips", "mmm", "xx", 
"panasonic", "sharp", "wap", "sch",
"rover", "pocket", "benq", "java", 
"pt", "pg", "vox", "amoi", 
"bird", "compal", "kg", "voda",
"sany", "kdd", "dbt", "sendo", 
"sgh", "gradi", "jb", "dddi", 
"moto", "iphone"
};

                //Loop through each item in the list created above 
                //and check if the header contains that text
                foreach (string s in mobiles)
                {
                    if (context.Request.ServerVariables["HTTP_USER_AGENT"].ToLower().Contains(s.ToLower()))
                    {
                        return true;
                    }
                }
            }

            return false;
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七七 2024-12-11 14:33:15

如果我不需要使用设备功能(通常是 WURFL),那么我会使用此代码。也许您可以使用此正则表达式来完成您的代码。

If I don´t need to use device capabilities (normaly WURFL), then I use this code. Maybe you could complete your code with this regex.

很糊涂小朋友 2024-12-11 14:33:15

好的,我找到了解决办法
我添加了更多检查

if (context.Request.UserAgent.ToLower().Contains("pc"))
            {
                return false;
            }

ok i found a work around
i added more check

if (context.Request.UserAgent.ToLower().Contains("pc"))
            {
                return false;
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文