• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

asp.net实现识别客户端浏览器或操作系统

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

1、通过 UserAgent

protected void Page_Load(object sender, EventArgs e)
  {
    //验证终端
    string agent = Request.Headers["User-Agent"];
    if (choose_net(agent))
    {
      Response.Redirect("/mobile/");
    }
  }

  //验证终端
  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;
    }
  }

2.考虑asp.net mvc

里面有for mobile的模版

3.获取浏览器的属性

复制代码 代码如下:
HttpBrowserCapabilities bc=Request.Browser; Response.Write("<script language='javascript'>alert('" + bc.Browser + "');</script>");

 4.js判断

<script type="text/javascript">
    var bro = navigator.userAgent;
    //alert(bro);
    // alert(bro.indexOf("Windows Phone"));
    // iPad 浏览器navigator.userAgent(包含iPad)
    if (bro.indexOf("NetFront") > 0 || bro.indexOf("UCWEB") > 0
      || bro.indexOf("iPhone") > 0 || bro.indexOf("Windows CE") > 0
      || bro.indexOf("MIDP-2.0") > 0 || bro.indexOf("Android") > 0
      || bro.indexOf("Opera Mini") > 0 || bro.indexOf("SymbianOS") > 0
      || bro.indexOf("Windows Phone") > 0) {
      window.open("Wap1/Index.aspx", "_top");
    }
    //    else if (bro.indexOf("Windows NT") > 0) {
    //      window.open("NewWeb/default.html", "_top");
    //    }
        else {
          window.open("NewWeb/default.html", "_top");
        }
  </script>

我们再来看下如何获取PC端的客户端信息呢

/// <summary>
/// 获取操作系统的名字
/// </summary>
/// <param name="userAgent"></param>
/// <returns></returns>
private string GetOSNameByUserAgent(string userAgent)
{
	string osVersion = "未知";
 
	if (userAgent.Contains("NT 10.0"))
	{
		osVersion = "Windows 10";
	}
	else if (userAgent.Contains("NT 6.3"))
	{
		osVersion = "Windows 8.1";
	}
	else if (userAgent.Contains("NT 6.2"))
	{
		osVersion = "Windows 8";
	}
	else if (userAgent.Contains("NT 6.1"))
	{
		osVersion = "Windows 7";
	}
	else if (userAgent.Contains("NT 6.1"))
	{
		osVersion = "Windows 7";
	}
	else if (userAgent.Contains("NT 6.0"))
	{
		osVersion = "Windows Vista/Server 2008";
	}
	else if (userAgent.Contains("NT 5.2"))
	{
		if (userAgent.Contains("64"))
			osVersion = "Windows XP";
		else
			osVersion = "Windows Server 2003";
	}
	else if (userAgent.Contains("NT 5.1"))
	{
		osVersion = "Windows XP";
	}
	else if (userAgent.Contains("NT 5"))
	{
		osVersion = "Windows 2000";
	}
	else if (userAgent.Contains("NT 4"))
	{
		osVersion = "Windows NT4";
	}
	else if (userAgent.Contains("Me"))
	{
		osVersion = "Windows Me";
	}
	else if (userAgent.Contains("98"))
	{
		osVersion = "Windows 98";
	}
	else if (userAgent.Contains("95"))
	{
		osVersion = "Windows 95";
	}
	else if (userAgent.Contains("Mac"))
	{
		osVersion = "Mac";
	}
	else if (userAgent.Contains("Unix"))
	{
		osVersion = "UNIX";
	}
	else if (userAgent.Contains("Linux"))
	{
		osVersion = "Linux";
	}
	else if (userAgent.Contains("SunOS"))
	{
		osVersion = "SunOS";
	}
	else
	{
		osVersion = HttpContext.Current.Request.Browser.Platform;
	}
	return osVersion;
}

基本上很全了,小伙伴们根据需求自由选择吧


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Asp.Net MVC3.0如何项目部署到Win7 64位系统发布时间:2022-02-05
下一篇:
ASP.NET MVC 3仿Server.Transfer效果的实现方法发布时间:2022-02-05
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap