要了解某个网站是在移动设备上打开的还是在pc web浏览器中打开的,我们可以有以下综合的几种方式来搞定:
通过判断Request.UserAgent中的具体信息来分析判断,因为UserAgent包含了很多的信息,所以我们可以根据其中的某些信息来进行判断,具体的判断方式如下:
(1)string strUserAgent = Request.UserAgent.ToString().ToLower(); if (strUserAgent != null){ if (Request.Browser.IsMobileDevice == true || strUserAgent.Contains("iphone") || strUserAgent.Contains("blackberry") || strUserAgent.Contains("mobile") || strUserAgent.Contains("windows ce") || strUserAgent.Contains("opera mini") || strUserAgent.Contains("palm")){ Response.Redirect("DefaultMobile.aspx"); } } (2)((System.Web.Configuration.HttpCapabilitiesBase)Request.Browser).IsMobileDevice (3)public static boolean choose(String userAgent){ Agent.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("Oper") > -1){ return true; } else{ return false; } (4)if (HttpContext.Current.Request.Headers["user-agent"] != null && HttpContext.Current.Request.Headers["user-agent"].ToLower().ToString().IndexOf("mozilla") != -1){ HttpContext.Current.Response.Redirect("http://www. "); //PC }else{ HttpContext.Current.Response.Redirect("http://wap "); //Mobile ClientTypeTesting. }
当然我们还可以根据设备的分辨率和屏幕大小来判断是移动设备还是pc设备,但是这个准确性比较低。
优酷的站点是通过网络的Ip地址来进行判断的,如果是移动网络,浏览器访问站点时就会跳转到3GP网站,否则就跳转到PC站点。