使用 HttpBrowserCapabilities 类轻松实现。HttpBrowserCapabilities 的名称空间是:
System.Web该名称空间默认是导入的。
示例
void Page_Load(object sender, EventArgs e)
{
HttpBrowserCapabilities bc = Request.Browser;
list.Text = "";
list.Text += "操作系统:" + bc.Platform. + "";
list.Text += "是否是 Win16 系统:" + bc.Win16 + "";
list.Text += "是否是 Win32 系统:" + bc.Win32 + "";
list.Text += "---";
list.Text += "浏览器:" + bc.Browser + "";
list.Text += "浏览器标识:" + bc.Id + "";
list.Text += "浏览器版本:" + bc.Version + "";
list.Text += "浏览器 MajorVersion:" + bc.MajorVersion.ToString() + "";
list.Text += "浏览器 MinorVersion:" + bc.MinorVersion.ToString() + "";
list.Text += "浏览器是否是测试版本:" + bc.Beta.ToString() + "";
list.Text += "是否是 America Online 浏览器:" + bc.AOL + "";
list.Text += "客户端安装的 .NET Framework 版本:" + bc.ClrVersion + "";
//即使安装了 .NET Framework,如果不是 IE 浏览器,检测版本都是 0.0。
list.Text += "是否是搜索引擎的网络爬虫:" + bc.Crawler + "";
list.Text += "是否是移动设备:" + bc.IsMobileDevice + "";
list.Text += "---";
list.Text += "显示的颜色深度:" + bc.ScreenBitDepth + ";
list.Text += "显示的近似宽度(以字符行为单位):" + bc.ScreenCharactersWidth + "";
list.Text += "显示的近似高度(以字符行为单位):" + bc.ScreenCharactersHeight + "";
list.Text += "显示的近似宽度(以像素行为单位):" + bc.ScreenPixelsWidth + "";
list.Text += "显示的近似高度(以像素行为单位):" + bc.ScreenPixelsHeight + "";
list.Text += "---";
list.Text += "是否支持 CSS:" + bc.SupportsCss + "";
list.Text += "是否支持 ActiveX 控件:" + bc.ActiveXControls.ToString() + ";
list.Text += "是否支持 JavaApplets:" + bc.JavaApplets.ToString() + "";
list.Text += "是否支持 JavaScript:" + bc.JavaScript.ToString() + "";
list.Text += "JScriptVersion:" + bc.JScriptVersion.ToString() + "";
list.Text += "是否支持 VBScript:" + bc.VBScript.ToString() + "";
list.Text += "是否支持 Cookies:" + bc.Cookies + "";
list.Text += "支持的 MSHTML 的 DOM 版本:" + bc.MSDomVersion + "";
list.Text += "支持的 W3C 的 DOM 版本:" + bc.W3CDomVersion + "";
list.Text += "是否支持通过 HTTP 接收 XML:" + bc.SupportsXmlHttp + "";
list.Text += "是否支持框架:" + bc.Frames.ToString() + "";
list.Text += "超链接 a 属性 href 值的最大长度:" + bc.MaximumHrefLength + "";
list.Text += "是否支持表格:" + bc.Tables + "";
}
返回值可从设定的字符大小和实际屏幕像素大小推出。确定高度的算法使用以下三项的组合:默认字体大小(来自 .config 文件);浏览器特定大小(也来自 .config 文件);浏览器发送的显式标头。某些浏览器可能依赖内部默认值,这些值只是近似于实际高度。
Request.Browser.MajorVersion.ToString();//获取客户端浏览器的(主)版本号Request.Browser.Version.ToString();//获取客户端浏览器的完整版本号
Request.Browser.Platform.ToString();//获取客户端使用平台的名字
Request.UserHostAddress.ToString(); //获取远程客户端主机IP
Request.UserHostAddress;
Request.Browser.Version;
Request.Browser.Platform;
HttpRequest.Url 获取有关当前请求的 URL 的信息。
HttpRequest.UrlReferrer 获取有关客户端上次请求的 URL 的信息,该请求链接到当前的 URL。
HttpRequest.UserAgent 获取客户端浏览器的原始用户代理信息。
HttpRequest.UserHostAddress 获取远程客户端的 IP 主机地址。
HttpRequest.UserHostName 获取远程客户端的 DNS 名称。
HttpRequest.UserLanguages 获取客户端语言首选项的排序字符串数组。
HttpRequest.UserAgent 获取客户端浏览器的原始用户代理信息。
HttpRequest.UserHostAddress 获取远程客户端的 IP 主机地址。
HttpRequest.UserHostName 获取远程客户端的 DNS 名称
HttpBrowserCapabilities bc = Request.Browser;
Response.Write("浏览器信息:");
Response.Write("浏览器= " + bc.Browser + "");
Response.Write("型态= " + bc.Type + "");
Response.Write("名称= " + bc.Browser + "");
Response.Write("版本= " + bc.Version + "");
Response.Write("使用平台= " + bc.Platform + "");
Response.Write("是否为测试版= " + bc.Beta + "");
Response.Write("是否为16 位的环境= " + bc.Win16 + "");
Response.Write("是否为32 位的环境= " + bc.Win32 + "");
Response.Write("是否支持框架(frame) = " + bc.Frames + "");
Response.Write("是否支持表格(table) = " + bc.Tables + "");
Response.Write("是否支持cookie = " + bc.Cookies + "");
Response.Write("是否支持vb script = " + bc.VBScript + "");
Response.Write("是否支持java script = " + bc.JavaScript + "");
Response.Write("是否支持java applets = " + bc.JavaApplets + "");
Response.Write("是否支持activex controls = " + bc.ActiveXControls + "");
if (Page.Request.Browser.EcmaScriptVersion.Major > 0 && Page.Request.Browser.W3CDomVersion.Major > 0)//判断是否支持脚本
{
Response.Write("浏览器名称与版本号:" + Page.Request.Browser.Type + "
");
Response.Write("浏览器版本号:" + Page.Request.Browser.Version + "
");
Response.Write("浏览器主版本号:" + Page.Request.Browser.MajorVersion + "
");
Response.Write("浏览器名称:" + Page.Request.Browser.Browser);
}
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class WebStart : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{ }
//获取浏览器版本号
public string getBrowser() {
string browsers;
HttpBrowserCapabilities bc = HttpContext.Current.Request.Browser;
string aa = bc.Browser.ToString();
string bb = bc.Version.ToString();
browsers = aa + bb;
return browsers;
}
//获取客户端IP地址
public string getIP()
{
string result = String.Empty;
result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (null == result || result == String.Empty)
{
result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
if (null == result || result == String.Empty)
{
result = HttpContext.Current.Request.UserHostAddress;
}
if (null == result || result == String.Empty)
{
return "0.0.0.0";
}
return result;
}
//获取操作系统版本号
public static string SystemCheck()
{
string Agent = HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"];
if (Agent.IndexOf("NT 4.0") > 0)
{
return "Windows NT ";
}
else if (Agent.IndexOf("NT 5.0") > 0)
{
return "Windows 2000";
}
else if (Agent.IndexOf("NT 5.1") > 0)
{
return "Windows XP";
}
else if (Agent.IndexOf("NT 5.2") > 0)
{
return "Windows 2003";
}
else if (Agent.IndexOf("NT 6.0") > 0)
{
return "Windows Vista";
}
else if (Agent.IndexOf("WindowsCE") > 0)
{
return "Windows CE";
}
else if (Agent.IndexOf("NT") > 0)
{
return "Windows NT ";
}
else if (Agent.IndexOf("9x") > 0)
{
return "Windows ME";
}
else if (Agent.IndexOf("98") > 0)
{
return "Windows 98";
}
else if (Agent.IndexOf("95") > 0)
{
return "Windows 95";
}
else if (Agent.IndexOf("Win32") > 0)
{
return "Win32";
}
else if (Agent.IndexOf("Linux") > 0)
{
return "Linux";
}
else if (Agent.IndexOf("SunOS") > 0)
{
return "SunOS";
}
else if (Agent.IndexOf("Mac") > 0)
{
return "Mac";
}
else if (Agent.IndexOf("Linux") > 0)
{
return "Linux";
}
else if (Agent.IndexOf("Windows") > 0)
{
return "Windows";
}
return "未知类型";
}
}
ASP.NET获取客户端的操作系统、浏览器、.NET版本代码如下:
protected void Page_Load(object sender, EventArgs e)
{
// 将客户端的信息添加到 Dictionary 中
Dictionary clientInfos = new Dictionary();
try
{
string ip = Request.UserHostAddress;
string userAgent = Request.UserAgent == null ? "无" : Request.UserAgent;
if(Request.ServerVariables["HTTP_UA_CPU"] == null)
clientInfos.Add("CPU 类型", "未知");
else
clientInfos.Add("CPU 类型", Request.ServerVariables["HTTP_UA_CPU"]);
clientInfos.Add("操作系统", this.GetOSNameByUserAgent(userAgent));
clientInfos.Add("IP 地址", ip);
if(Request.Browser.ClrVersion == null)
clientInfos.Add(".NET CLR 版本", "不支持");
else
clientInfos.Add(".NET CLR 版本", Request.Browser.ClrVersion.ToString());
clientInfos.Add("浏览器", Request.Browser.Browser + Request.Browser.Version);
clientInfos.Add("支持 ActiveX", Request.Browser.ActiveXControls ? "支持" : "不支持");
clientInfos.Add("支持 Cookies", Request.Browser.Cookies ? "支持" : "不支持");
clientInfos.Add("支持 CSS", Request.Browser.SupportsCss ? "支持" : "不支持");
clientInfos.Add("语言", Request.UserLanguages[0]);
string httpAccept = Request.ServerVariables["HTTP_ACCEPT"];
if (httpAccept == null)
clientInfos.Add("计算机/手机", "未知");
else if (httpAccept.IndexOf("wap") > -1)
clientInfos.Add("计算机/手机", "手机");
else
clientInfos.Add("计算机/手机", "计算机");
clientInfos.Add("Platform", Request.Browser.Platform);
clientInfos.Add("Win16", Request.Browser.Win16 ? "是" : "不是");
clientInfos.Add("Win32", Request.Browser.Win32 ? "是" : "不是");
if(Request.ServerVariables["HTTP_ACCEPT_ENCODING"] == null)
clientInfos.Add("Http Accept Encoding", "无");
else
clientInfos.Add("Http Accept Encoding", Request.ServerVariables["HTTP_ACCEPT_ENCODING"]);
clientInfos.Add("User Agent", userAgent);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
// 根据 Dictionary 中的内容在 Table 中显示客户端信息
this.Response.Write("
项目 | 值 |
" + key + " | " + clientInfos[key].ToString() + " |
这是是获取操作系统名称的代码:
根据User Agent获取操作系统名称
///
/// 根据 User Agent 获取操作系统名称
///
private string GetOSNameByUserAgent(string userAgent)
{
string osVersion = "未知";
if (userAgent.Contains("NT 6.0"))
{
osVersion = "Windows Vista/Server 2008";
}
else if (userAgent.Contains("NT 5.2"))
{
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";
}
return osVersion;
}
···
···