欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-22 13:41:22
1、通过 useragent protected void page_load(object sender, eventargs e) { /...

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;
}

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

上一篇:

下一篇: