德育量化管理系统

特色功能

ASP.NET技术

ASP.NET怎样判断网页是手机登录访问还是电脑访问

作者:admin 来源: 人气: 标签:asp.net 判断 手机 电脑 访问 登录

asp.net怎样实现判断是手机访问网站还是电脑访问网站,随着智能手机的发展,手机越来越被广大用户使用,当用户输入手机浏览器网址时,如果正常访问正常的电脑网站则要耗费很大的流量,所以特此开发了手机网站,但不可能专门再指定个域名进行推广,而且很多用户也不知道,大部分用户还是习惯于输入例如www.schoolms.net ,但如果这样访问的话将耗费很大流量,所以特此指定手机网站,那么怎样判断是手机访问还是电脑访问,下面为大家实现了asp.net判断是手机访问还是电脑访问代码,供大家参考;


        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write(Request.Headers["User-Agent"].ToString() + "<br/>");
            //Response.Write()
            string agent=Request.Headers["User-Agent"];
            if (choose_net(agent))
            {
                Response.Write("手机");
            }
            else
            {
                Response.Write("电脑");
            }

        }
//对用户访问进行判断 如果是手机的话返回true,否则返回false
        public bool choose_net(String userAgent)
        {
            if (userAgent.IndexOf("Noki") > -1 || // Nokia phones and emulators    
                     userAgent.IndexOf("Eric") > -1 || // Ericsson WAP phones and emulators    
                     userAgent.IndexOf("WapI") > -1 || // Ericsson WapIDE 2.0    
                     userAgent.IndexOf("MC21") > -1 || // Ericsson MC218    
                     userAgent.IndexOf("AUR") > -1 || // Ericsson R320    
                     userAgent.IndexOf("R380") > -1 || // Ericsson R380    
                     userAgent.IndexOf("UP.B") > -1 || // UP.Browser    
                     userAgent.IndexOf("WinW") > -1 || // WinWAP browser    
                     userAgent.IndexOf("UPG1") > -1 || // UP.SDK 4.0    
                     userAgent.IndexOf("upsi") > -1 || //another kind of UP.Browser    
                     userAgent.IndexOf("QWAP") > -1 || // unknown QWAPPER browser    
                     userAgent.IndexOf("Jigs") > -1 || // unknown JigSaw browser    
                     userAgent.IndexOf("Java") > -1 || // unknown Java based browser    
                     userAgent.IndexOf("Alca") > -1 || // unknown Alcatel-BE3 browser (UP based)    
                     userAgent.IndexOf("MITS") > -1 || // unknown Mitsubishi browser    
                     userAgent.IndexOf("MOT-") > -1 || // unknown browser (UP based)    
                     userAgent.IndexOf("My S") > -1 ||//  unknown Ericsson devkit browser     
                     userAgent.IndexOf("WAPJ") > -1 ||//Virtual WAPJAG www.wapjag.de    
                     userAgent.IndexOf("fetc") > -1 ||//fetchpage.cgi Perl script from www.wapcab.de    
                     userAgent.IndexOf("ALAV") > -1 || //yet another unknown UP based browser    
                     userAgent.IndexOf("Wapa") > -1 || //another unknown browser (Web based "Wapalyzer")   
                     userAgent.IndexOf("UCWEB") > -1 || //another unknown browser (Web based "Wapalyzer")   
                     userAgent.IndexOf("BlackBerry") > -1 || //another unknown browser (Web based "Wapalyzer")                    
                     userAgent.IndexOf("J2ME") > -1 || //another unknown browser (Web based "Wapalyzer")             
                     userAgent.IndexOf("Oper") > -1 ||
                     userAgent.IndexOf("Android") > -1||
                userAgent.IndexOf("mozilla")>-1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
以上代码已测试可用正常使用,以上是为大家讲解怎样判断是手机访问网站还是电脑访问,手机访问跳转到手机wap网站,如果是电脑访问的话正常显示,供大家参考学习。