您的位置:首页 > 编程语言 > ASP

ASP.NET 中获取客户端浏览器信息

2010-08-16 11:02 621 查看
使用 HttpBrowserCapabilities 类轻松实现。

HttpBrowserCapabilities 的名称空间是:

System.Web
该名称空间默认是导入的。

示例

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
HttpBrowserCapabilities bc = Request.Browser;

list.Text = "";
list.Text += "操作系统:" + bc.Platform + "<br>";
list.Text += "是否是 Win16 系统:" + bc.Win16 + "<br>";
list.Text += "是否是 Win32 系统:" + bc.Win32 + "<br>";
list.Text += "---<br>";

list.Text += "浏览器:" + bc.Browser + "<br>";
list.Text += "浏览器标识:" + bc.Id + "<br>";
list.Text += "浏览器版本:" + bc.Version + "<br>";
list.Text += "浏览器 MajorVersion:" + bc.MajorVersion.ToString() + "<br>";
list.Text += "浏览器 MinorVersion:" + bc.MinorVersion.ToString() + "<br>";
list.Text += "浏览器是否是测试版本:" + bc.Beta.ToString() + "<br>";
list.Text += "是否是 America Online 浏览器:" + bc.AOL + "<br>";
list.Text += "客户端安装的 .NET Framework 版本:" + bc.ClrVersion + "<br>"; //即使安装了 .NET Framework,如果不是 IE 浏览器,检测版本都是 0.0。
list.Text += "是否是搜索引擎的网络爬虫:" + bc.Crawler + "<br>";
list.Text += "是否是移动设备:" + bc.IsMobileDevice + "<br>";
list.Text += "---<br>";

list.Text += "显示的颜色深度:" + bc.ScreenBitDepth + "<br>";
list.Text += "显示的近似宽度(以字符行为单位):" + bc.ScreenCharactersWidth + "<br>";
list.Text += "显示的近似高度(以字符行为单位):" + bc.ScreenCharactersHeight + "<br>";
list.Text += "显示的近似宽度(以像素行为单位):" + bc.ScreenPixelsWidth + "<br>";
list.Text += "显示的近似高度(以像素行为单位):" + bc.ScreenPixelsHeight + "<br>";
list.Text += "---<br>";

list.Text += "是否支持 CSS:" + bc.SupportsCss + "<br>";
list.Text += "是否支持 ActiveX 控件:" + bc.ActiveXControls.ToString() + "<br>";
list.Text += "是否支持 JavaApplets:" + bc.JavaApplets.ToString() + "<br>";
list.Text += "是否支持 JavaScript:" + bc.JavaScript.ToString() + "<br>";
list.Text += "JScriptVersion:" + bc.JScriptVersion.ToString() + "<br>";
list.Text += "是否支持 VBScript:" + bc.VBScript.ToString() + "<br>";
list.Text += "是否支持 Cookies:" + bc.Cookies + "<br>";
list.Text += "支持的 MSHTML 的 DOM 版本:" + bc.MSDomVersion + "<br>";
list.Text += "支持的 W3C 的 DOM 版本:" + bc.W3CDomVersion + "<br>";
list.Text += "是否支持通过 HTTP 接收 XML:" + bc.SupportsXmlHttp + "<br>";
list.Text += "是否支持框架:" + bc.Frames.ToString() + "<br>";
list.Text += "超链接 a 属性 href 值的最大长度:" + bc.MaximumHrefLength + "<br>";
list.Text += "是否支持表格:" + bc.Tables + "<br>";
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ASP.NET 中如何获取客户端浏览器信息 - 千一网络</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="list" runat="server"></asp:Label>
</div>
</form>
</body>
</html>

ScreenCharactersWidth、ScreenCharactersHeight、ScreenPixelsWidth、ScreenPixelsHeight 并不能确定屏幕分辨率,在http://msdn2.microsoft.com/zh-cn/library/system.web.configuration.httpcapabilitiesbase.screencharactersheight(VS.80).aspx可以看到:

返回值可从设定的字符大小和实际屏幕像素大小推出。确定高度的算法使用以下三项的组合:默认字体大小(来自 .config 文件);浏览器特定大小(也来自 .config 文件);浏览器发送的显式标头。某些浏览器可能依赖内部默认值,这些值只是近似于实际高度。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: