60

是否有用于创建用户代理字符串的 RFC、官方标准或模板?iphone的用户代理字符串似乎很奇怪......

Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_1_2 like Mac OS X ; en-us) AppleWebKit/528.18 ( KHTML, like Gecko ) Version/4.0 Mobile/7D11 Safari/528.16

4

3 回答 3

77

User-Agent头是 的一部分RFC7231,它是 的改进版本RFC1945,其中指出:

User-Agentrequest-header 字段包含有关发起请求的用户代理的信息。这是出于统计目的、协议违规的跟踪以及用户代理的自动识别,以便定制响应以避免特定的用户代理限制。用户代理应该在请求中包含这个字段。该字段可以包含多个product令牌(第 3.8 节)并comments标识代理和构成用户代理重要部分的任何子产品。按照惯例,product令牌是按照它们在识别应用程序中的重要性的顺序列出的。

EBNF 定义:

   User-Agent      = "User-Agent" ":" 1*( product | comment )

其中product定义为:

   product         = token ["/" product-version]
   product-version = token
   token           = 1*<any CHAR except CTLs or separators>

并且comment作为:

   comment         = "(" *( ctext | quoted-pair | comment ) ")"
   ctext           = <any TEXT excluding "(" and ")">

及其他规则,供参考:

   CTL             = <control characters, e.g. ASCII 0x00 through 0x0F and 0x7F>
   separators      = "(" | ")" | "<" | ">" | "@"
                     "," | ";" | ":" | "\" | <">
                     "/" | "[" | "]" | "?" | "="
                     "{" | "}" | SP | HT
   SP              = <ASCII space 0x20, i.e. " ">
   HT              = <ASCII horizontal tab 0x09, aka '\t'>

请注意,这意味着product字符串不能包含空格,但comment字符串可以。


例子:

以下是一些有效的product字符串示例(带和不带product-version字符串):

# Single `product` without product-version:
Foobar
Foobar-baz

# Single `product` with product-version:
Foobar/abc
Foobar/1.0.0
Foobar/2021.44.30.15-b917dc

以下是一些有效的comment字符串示例;请注意如何将所有字符串括在匹配的括号中( )

# This was the default `comment` used by Internet Explorer 11:
(Windows NT 6.1; WOW64; Trident/7.0; rv:11.0)

# You can put almost any text inside a comment:
(Why are you looking at HTTP headers? Go outside, find love, do some good in the world)

# Note that `comment` strings can also be nested, provided their delimiting parentheses are matched, for example:
(Outer comment (Inner comment))

由于User-Agent标头的值由任意productcomment字符串组成,因此这些都是有效的User-Agent标头:

User-Agent: Foobar
User-Agent: Foobar/2021.44.30.15-b917dc
User-Agent: MyProduct Foobar/2021.44.30.15-b917dc
User-Agent: Tsom/OfraHaza (Life is short and love is always over in the morning) AnotherProduct
于 2010-04-08T16:06:03.260 回答
11

这在RFC 1945的请求标头部分中指定。不过,这不是一种非常标准化的格式,用户代理倾向于将他们想要的任何东西放在那里。

于 2010-04-08T15:57:14.513 回答
3

是的,请参阅:Mozilla 网站,但正如之前提到的那样。基本上你可以放任何你想要的东西。出于统计/分析目的,最重要的是,每个浏览器/操作系统都应该为自己标准化。

于 2010-04-08T15:59:49.763 回答