10

下面是我的代码:

    User_Info.Add(!string.IsNullOrEmpty(Request.UserAgent) ? Request.UserAgent : string.Empty);//4:UserAgent

    HttpBrowserCapabilities browser = Request.Browser;
    User_Info.Add(!string.IsNullOrEmpty(browser.Browser) ?  "Name : " + browser.Browser + " | " +
                                                            "Type : " + browser.Type + " | " +
                                                            "MajorVersion  : " + browser.MajorVersion + " | " +
                                                            "MinorVersion  : " + browser.MinorVersion : string.Empty);//5:UserBrowser

Request.UserAgent 和 Request.Browser 有什么区别?
我无法理解那些 UserAgent 字符串!
你能举一些例子和解释吗?

4

3 回答 3

14

UserAgent为您提供有关浏览器的原始字符串。它可能看起来像这样:

用户代理 :: Mozilla/4.0(兼容;MSIE 6.0b;Windows NT 5.1;.NET CLR 1.0.2914)

Request.Browser将为您提供 HttpBrowserCapabilities 对象,该对象将包含浏览器版本信息以及有关浏览器功能的一些额外信息。例如:

  1. 浏览器是否支持 Frames
  2. 如果它支持cookies?
  3. 支持 JavaScript 吗?
  4. 支持 Java 小程序 ? 等等

看下面的示例代码:

HttpBrowserCapabilities bc = Request.Browser;
Response.Write("<p>Browser Capabilities:</p>");
Response.Write("Type = " + bc.Type + "<br>");
Response.Write("Name = " + bc.Browser + "<br>");
Response.Write("Version = " + bc.Version + "<br>");
Response.Write("Major Version = " + bc.MajorVersion + "<br>");
Response.Write("Minor Version = " + bc.MinorVersion + "<br>");
Response.Write("Platform = " + bc.Platform + "<br>");
Response.Write("Is Beta = " + bc.Beta + "<br>");
Response.Write("Is Crawler = " + bc.Crawler + "<br>");
Response.Write("Is AOL = " + bc.AOL + "<br>");
Response.Write("Is Win16 = " + bc.Win16 + "<br>");
Response.Write("Is Win32 = " + bc.Win32 + "<br>");
Response.Write("Supports Frames = " + bc.Frames + "<br>");
Response.Write("Supports Tables = " + bc.Tables + "<br>");
Response.Write("Supports Cookies = " + bc.Cookies + "<br>");
Response.Write("Supports VB Script = " + bc.VBScript + "<br>");
Response.Write("Supports JavaScript = " + bc.JavaScript + "<br>");
Response.Write("Supports Java Applets = " + bc.JavaApplets + "<br>");
Response.Write("Supports ActiveX Controls = " + bc.ActiveXControls + "<br>");
Response.Write("CDF = " + bc.CDF + "<br>");

为了将浏览器版本与用户代理进行比较,您必须使用字符串操作(包含),而如果Request.Browser您可以与属性进行比较。

于 2012-05-28T07:42:38.080 回答
8

Request.Browser 与 Request.UserAgent 不同。UserAgent 获取客户端浏览器的原始用户代理字符串,Request.Browser获取有关浏览器功能的信息。您不会使用UserAgent 字符串获得所有浏览器功能。

于 2012-05-28T06:53:09.617 回答
1

Request.UserAgent有点神秘,需要解析以确定访问者正在使用的浏览器。此外,它不包含浏览器支持的 JavaScript 版本,或者浏览器是否支持 CSS 2.0 样式表等信息

Request.Browser属性是 HttpBrowserCapabilities 对象的一个​​实例,提供所有信息...

参考:http ://www.4guysfromrolla.com/articles/120402-1.aspx

于 2012-05-28T07:05:10.420 回答