C# Request.UserAgent 和 Request.Browser 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10780418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 15:14:22  来源:igfitidea点击:

What is the difference between Request.UserAgent and Request.Browser?

c#asp.netrequestuser-agent

提问by SilverLight

Here is my code below :

这是我的代码如下:

    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

What is the difference between Request.UserAgent and Request.Browser?
I couldn't understand those UserAgent strings!
Would you please show some examples with explanation?

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

采纳答案by Asif Mushtaq

Request.Browser is different from Request.UserAgent. UserAgent gets the raw user agent string of the client browser, and Request.Browsergets you the information about the browser capabilities. You wont get all the browser capabilities with the UserAgent string.

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

回答by arun.v1

Request.UserAgentis a bit cryptic, and requires parsing to determine what browser, specifically, a visitor is using. Furthermore, it doesn't contain information like what version of JavaScript the browser supports, or if the browser supports CSS 2.0 stylesheets

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

The Request.Browserproperty is an instance of the HttpBrowserCapabilities object provides all the information...

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

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

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

回答by Habib

UserAgentgives you a raw string about the browser. It might look like this:

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

User Agent :: Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 5.1; .NET CLR 1.0.2914)

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

Request.Browserwill give you HttpBrowserCapabilities object which will have browser version information along with some extra information regarding the capabilities of the browser. For example:

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

  1. Whether browser supports Frames
  2. If it supports cookies ?
  3. Supports JavaScripts ?
  4. Supports Java Applets ? etc.
  1. 浏览器是否支持Frames
  2. 如果它支持cookies?
  3. 支持 JavaScript 吗?
  4. 支持 Java 小程序吗?等等。

Look at the following sample code:

看下面的示例代码:

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>");

For comparing browser version against a user agent, you would have to use string operations (Contains), whereas in case of Request.Browseryou can compare against a property.

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