陈斌彬的技术博客

Stay foolish,stay hungry

asp.net获取客户端浏览器及主机信息

在线预览效果:

http://tool.hovertree.com/info/client/

其中aspx页面的控件代码:

<asp:ListBox runat="server" ID="lbHoverTreeInfo" ClientIDMode="Static"> </asp:ListBox>

以下是实现代码:

  1 using System;
  2 using System.Web;
  3 using System.Web.UI;
  4 
  5 namespace HoverTreeTool.HvtInfo.KeleyiClient
  6 {
  7 public partial class Index : System.Web.UI.Page
  8 {
  9 protected void Page_Load(object sender, EventArgs e)
 10 {
 11 string userAgent = Request.UserAgent == null ? "无" : Request.UserAgent;
 12 if (!IsPostBack)
 13 {lbHoverTreeInfo.Items.Add("您的系统信息为:");
 14 lbHoverTreeInfo.Items.Add("客户端IP[Page.Request.UserHostAddress]:"+ Page.Request.UserHostAddress);
 15 lbHoverTreeInfo.Items.Add("浏览器类型[Request.Browser.Browser]:" + Request.Browser.Browser);
 16 lbHoverTreeInfo.Items.Add("浏览器标识[Request.Browser.Id]:" + Request.Browser.Id);
 17 lbHoverTreeInfo.Items.Add("浏览器版本号[Request.Browser.Version]:" + Request.Browser.Version);
 18 lbHoverTreeInfo.Items.Add("浏览器是不是测试版本[Request.Browser.Beta]:" + Request.Browser.Beta);
 19 lbHoverTreeInfo.Items.Add("浏览器类型[Request.Browser.Type]:" + Request.Browser.Type);
 20 lbHoverTreeInfo.Items.Add("是否支持框架网页[Request.Browser.Frames]:" + Request.Browser.Frames);
 21 lbHoverTreeInfo.Items.Add("是否支持Cookie[Request.Browser.Cookies]:" + Request.Browser.Cookies);
 22 lbHoverTreeInfo.Items.Add("浏览器JScript版本[Request.Browser.JScriptVersion]:" + Request.Browser.JScriptVersion);
 23 lbHoverTreeInfo.Items.Add("屏幕分辨率宽[System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width ]]:" + System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width);
 24 lbHoverTreeInfo.Items.Add("屏幕分辨率高[System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height ]]:" + System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height);
 25 lbHoverTreeInfo.Items.Add("浏览器类型[Request.Browser.Type]:" + Request.Browser.Type);
 26 lbHoverTreeInfo.Items.Add("客户端IP[GetHoverTreeIp()]:" + GetHoverTreeIp());
 27 lbHoverTreeInfo.Items.Add("客户端的操作系统[Request.Browser.Platform]:" + Request.Browser.Platform);
 28 lbHoverTreeInfo.Items.Add("客户端的操作系统[GetHoverTreeOSName(userAgent)]:" + GetHoverTreeOSName(userAgent));
 29 lbHoverTreeInfo.Items.Add("是不是win16系统[Request.Browser.Win16]:" + Request.Browser.Win16);
 30 lbHoverTreeInfo.Items.Add("是不是win32系统[Request.Browser.Win32]:" + Request.Browser.Win32);
 31 lbHoverTreeInfo.Items.Add("客户端.NET Framework版本:Request.Browser.ClrVersion]:" + Request.Browser.ClrVersion);
 32 lbHoverTreeInfo.Items.Add("是否支持Java[Request.Browser.JavaApplets]:" + Request.Browser.JavaApplets);
 33 
 34 if (Request.ServerVariables["HTTP_UA_CPU"] == null)
 35 lbHoverTreeInfo.Items.Add("CPU 类型[Request.ServerVariables[\"HTTP_UA_CPU\"]]:未知");
 36 else
 37 lbHoverTreeInfo.Items.Add("CPU 类型[Request.ServerVariables[\"HTTP_UA_CPU\"]]:" + Request.ServerVariables["HTTP_UA_CPU"]);
 38 
 39 lbHoverTreeInfo.Items.Add("UserAgent信息[Request.UserAgent]:" + userAgent);
 40 lbHoverTreeInfo.Items.Add("By 何问起工具 http://tool.hovertree.com/info/client/");
 41 
 42 }
 43 }
 44 
 45 /// <summary>
 46 /// 获取真实IP
 47 /// </summary>
 48 /// <returns></returns>
 49 public string GetHoverTreeIp()
 50 {
 51 string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
 52 if (null == result || result == String.Empty)
 53 {
 54 result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
 55 }
 56 if (null == result || result == String.Empty)
 57 {
 58 result = HttpContext.Current.Request.UserHostAddress;
 59 }
 60 return result;
 61 }
 62 
 63 /// <summary>
 64 /// 根据 User Agent 获取操作系统名称
 65 /// </summary>
 66 private string GetHoverTreeOSName(string userAgent)
 67 {
 68 string m_hvtOsVersion = "未知";
 69 if (userAgent.Contains("NT 6.4"))
 70 {
 71 m_hvtOsVersion = "Windows 10";
 72 }
 73 else
 74 if (userAgent.Contains("NT 6.3"))
 75 {
 76 m_hvtOsVersion = "Windows 8.1";
 77 }
 78 else
 79 if (userAgent.Contains("NT 6.2"))
 80 {
 81 m_hvtOsVersion = "Windows 8";
 82 }
 83 else
 84 if (userAgent.Contains("NT 6.1"))
 85 {
 86 m_hvtOsVersion = "Windows 7";
 87 }else
 88 if (userAgent.Contains("NT 6.0"))
 89 {
 90 m_hvtOsVersion = "Windows Vista/Server 2008";
 91 }
 92 else if (userAgent.Contains("NT 5.2"))
 93 {
 94 m_hvtOsVersion = "Windows Server 2003";
 95 }
 96 else if (userAgent.Contains("NT 5.1"))
 97 {
 98 m_hvtOsVersion = "Windows XP";
 99 }
100 else if (userAgent.Contains("NT 5"))
101 {
102 m_hvtOsVersion = "Windows 2000";
103 }
104 else if (userAgent.Contains("NT 4"))
105 {
106 m_hvtOsVersion = "Windows NT4";
107 }
108 else if (userAgent.Contains("Me"))
109 {
110 m_hvtOsVersion = "Windows Me";
111 }
112 else if (userAgent.Contains("98"))
113 {
114 m_hvtOsVersion = "Windows 98";
115 }
116 else if (userAgent.Contains("95"))
117 {
118 m_hvtOsVersion = "Windows 95";
119 }
120 else if (userAgent.Contains("Mac"))
121 {
122 m_hvtOsVersion = "Mac";
123 }
124 else if (userAgent.Contains("Unix"))
125 {
126 m_hvtOsVersion = "UNIX";
127 }
128 else if (userAgent.Contains("Linux"))
129 {
130 m_hvtOsVersion = "Linux";
131 }
132 else if (userAgent.Contains("SunOS"))
133 {
134 m_hvtOsVersion = "SunOS";
135 }
136 return m_hvtOsVersion;
137 }
138 
139 }
140 }