Updated: July 2013
Modify by aerchi, 2016-06-17
原文地址: https://msdn.microsoft.com/library/ms537503(v=vs.85).aspx
Here we discuss the user-agent string, which identifies your browser and provides certain system details to servers hosting the websites you visit. We'll also learn how to view your user-agent string, understand tokens used by recent versions of Windows Internet Explorer, and understand registry keys that affect the user-agent string.
We'll cover these sections.
When you visit a webpage, your browser sends the user-agent string to the server hosting the site that you are visiting. This string indicates which browser you're using, its version number, and details about your system, such as operating system and version. The web server can use this information to provide content that is tailored for your specific browser.
Because certain non-Microsoft sites add details to the user-agent string, it's important to understand the user-agent string. Here we'll explain the user-agent string, list the values from recent Internet Explorer versions, and document registry keys that modify the user-agent string.
When you request a webpage, your browser sends a number of headers to the server hosting the site that you're visiting, as shown here.
GET / HTTP/1.1 Accept: text/html, application/xhtml+xml, */* Accept-Language: en-US User-Agent: Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko Accept-Encoding: gzip, deflate Proxy-Connection: Keep-Alive Host: microsoft.com
These headers occur during a negotiation process that helps the browser and the hosting server determine the best way to provide the requested information. The user-agent header identifies the application requesting the information from the server, typically a browser. This identification string is called the user-agent string and contains tokens that provide specific details about the program making the request. Tokens vary among programs; the tokens in the Internet Explorer user-agent string describe the browser, the operating system, and the current browser mode.
Starting with IE11, the user-agent string was updated to be more consistent with other popular browsers. To learn more, see Compatibility changes in IE11.
This table describes the tokens in a user-agent string shown in the example.
Token | Description |
---|---|
Mozilla/5.0 |
Application name and version. For historical reasons, Internet Explorer identifies itself as a Mozilla browser. |
Windows NT 6.3 |
The Platform token identifies the operating system and version. The example token indicates Windows 8.1. |
Trident/7.0 |
The Trident token identifies the version of MSHTML (Trident). |
rv:11.0 |
The revision token indicates the version of IE11 |
like Gecko |
The Gecko token has been added to highlight improved consistency with other browsers. |
Note that this example applies to IE11. Earlier versions of the browser reported user-agent strings like this:
Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
Token | Description |
---|---|
Mozilla/5.0 |
Application name and version. For historical reasons, Internet Explorer identifies itself as a Mozilla browser. |
compatible |
Compatibility flag token. It indicates that Internet Explorer is compatible with a set of common features. |
MSIE 9.0 |
The Version token identifies the browser and contains the version number, as reported by the current browser mode. The example value indicates Windows Internet Explorer 9. |
Windows NT 6.1 |
The Platform token identifies the operating system and version. The example token indicates Windows 7. |
Trident/5.0 |
The Trident token identifies the version of MSHTML (Trident) and can be used to determine whether or not the webpage is displayed in Compatibility View. |
For these examples, the user-agent is Internet Explorer. Other programs also act as user-agents and provide their own user-agent strings. For example, the Windows RSS Platform provides the following user-agent header when requesting RSS data.
Windows-RSS-Platform/2.0 (IE 11.0; Windows NT 6.1)
Note that the user-agent string changed as of IE11. Earlier versions of the Windows RSS Platform returned this string:
Windows-RSS-Platform/2.0 (MSIE 9.0; Windows NT 6.1)
Like the user-agent string for Internet Explorer, this user-agent header provides details about your system.
Alternate tokens can appear in the user-agent string for a number of reasons; usually, they identify optional features installed on your system. For example, an SV1 token appears in the user-agent string of a Windows XP user who has installed Windows XP Service Pack 2 (SP2). It's also important to note that certain non-Microsoft parties modify the user-agent string for their own purposes.
As of Internet Explorer 9, alternate tokens are handled differently from earlier versions of the browser. To learn more, see User-agent registry keys.
For info about other headers communicated to web servers, see RFC2616, Section 14: Header Field Definitions.
Applications can use the userAgent property of the navigator object to determine the user-agent string.
You can use this information to view the user-agent string. To do so, type the following into the address bar of your browser.
javascript:alert(navigator.userAgent)
This code example is case sensitive.
When you do this, a dialog box displays your user-agent string.
Tip: If you're using a recent version of Windows, you can copy your user-agent string to the Clipboard by pressing Ctrl+C before you close the dialog box.
For best results, user-agent strings should not be longer than 256 characters.
This section summarizes the tokens used by recent versions of Internet Explorer, which places three tokens in the user-agent string by default.
In general, version tokens begin with MSIE and are followed by the version number of the browser. Many pre-release versions append the letter "b" to the version number.
Starting with Windows Internet Explorer 8, the version token reported in the user-agent string might not reflect the actual version of the browser. IfCompatibility View is enabled for a webpage or the browser mode is set to an earlier version, the version token reports the earlier version.
For example, if you're using Internet Explorer 9 to view a webpage in Compatibility View, the version token is, by default, MSIE 7.0
.
This means that the version token represents the way Internet Explorer is presenting itself to the web server. As of Internet Explorer 8, the Trident tokenmore accurately represents the version of the browser.
This table lists the version tokens used in recent versions of Internet Explorer.
Version token | Description |
---|---|
MSIE 10.0 | Internet Explorer 10 |
MSIE 9.0 | Internet Explorer 9 |
MSIE 8.0 | Internet Explorer 8 or IE8 Compatibility View/Browser Mode |
MSIE 7.0 | Windows Internet Explorer 7 or IE7 Compatibility View/Browser Mode |
MSIE 6.0 | Microsoft Internet Explorer 6 |
Platform tokens describe your operating system. The following table lists Internet Explorer platform tokens for the last several versions of Windows.
Platform token | Description |
---|---|
Windows NT 10.0 | Windows 10 |
Windows NT 6.3 | Windows 8.1 |
Windows NT 6.2 | Windows 8 |
Windows NT 6.1 | Windows 7 |
Windows NT 6.0 | Windows Vista |
Windows NT 5.2 | Windows Server 2003; Windows XP x64 Edition |
Windows NT 5.1 | Windows XP |
Windows NT 5.01 | Windows 2000, Service Pack 1 (SP1) |
Windows NT 5.0 | Windows 2000 |
Windows NT 4.0 | Microsoft Windows NT 4.0 |
Windows 98; Win 9x 4.90 | Windows Millennium Edition (Windows Me) |
Windows 98 | Windows 98 |
Windows 95 | Windows 95 |
Windows CE | Windows CE |
Certain optional components can also modify the user-agent string; this next table shows common ones. Be aware that many other applications also modify the user-agent string. Because of this, a comprehensive list is not possible. If you find additional tokens in your user-agent string, investigate them in more detail.
Token | Description |
---|---|
.NET CLR | .NET Framework common language run time, followed by the version number. |
SV1 | Internet Explorer 6 with enhanced security features (Windows XP SP2 and Windows Server 2003 only). |
Tablet PC | Tablet services are installed; number indicates the version number. |
Win64; IA64 | System has a 64-bit processor (Intel). |
Win64; x64 | System has a 64-bit processor (AMD). |
WOW64 | A 32-bit version of Internet Explorer is running on a 64-bit processor. |
When the F12 developer tools are used to change the browser mode of Internet Explorer, the version token of the user-agent string is modified to appear so that the browser appears to be an earlier version. This is done to allow browser specific content to be served to Internet Explorer and is usually necessary only when websites have not been updated to reflect current versions of the browser.
When this happens, a Trident token is added to the user-agent string. This token includes a version number that enables you to identify the version of the browser, regardless of the current browser mode.
Token | Description |
---|---|
Edge/13.10586 | Windows Edge |
Trident/7.0 | IE11 |
Trident/6.0 | Internet Explorer 10 |
Trident/5.0 | Internet Explorer 9 |
Trident/4.0 | Internet Explorer 8 |
In general, public websites should rely on feature detection, rather than browser detection, in order to design their sites for browsers that don't support the features used by the website. For more info, see Detecting Internet Explorer More Effectively.
Sites that rely on user-agent strings to generate visitation statistics should update their algorithms to account for the Trident token; otherwise, visitation metrics might not accurately reflect the true composition of browser usage.
When you install certain third-party programs or Windows components, such as the Microsoft .NET Framework or Windows XP SP2, feature tokens are added to the user-agent string.
This is done by adding tokens to the following registry keys.
HKEY_LOCAL_MACHINE (or HKEY_CURRENT_USER) SOFTWARE Microsoft Windows CurrentVersion Internet Settings User Agent Pre Platform Token = Value Post Platform Token = Value
The Pre-Platform and Post-Platform keys contain values whose names appear before and after the Platform token, respectively. For example, if a string value is added to the Post-Platform key, the name appears after the platform token in the user-agent string. Multiple tokens added to either key appear in an unpredictable order.
Earlier versions of Internet Explorer included feature tokens defined using the Pre-Platform and Post-Platform keys part of the user-agent string during the HTTP negotiation process. Over time, this lead to overly long user-agent strings, which in turn created problems for certain web servers. Problems usually appeared when user-agent strings were longer than 256 characters. As of Internet Explorer 9, the user-agent string no longer includes feature tokens during HTTP negotiation. Feature tokens are included in the value returned by the userAgent property of the navigator object. Applications that rely on the earlier behavior should be modified accordingly.
You can also override certain tokens of the user-agent string by adding values to the following registry key.
HKEY_LOCAL_MACHINE (or HKEY_CURRENT_USER) SOFTWARE Microsoft Windows CurrentVersion Internet Settings 5.0 User Agent (default) = "Mozilla/4.0" Compatible = "compatible" Platform = "Windows NT 5.1" Version = "MSIE 6.0" Pre Platform Token = Value Post Platform Token = Value
The default value of the User Agent key replaces the application name and application version tokens reported in the user-agent string. Be aware that the first seven characters are used for the application name, and the remaining characters specify the application version token.
The Compatible, Platform, and Version values replace the corresponding tokens in the user-agent string.
Additional tokens can be added to the user-agent string by using the Registry Editor to create new string values under the Pre-Platform key or Post-Platform key. The value name should be the complete token; the value data is ignored. Tokens added to the Pre-Platform key appear before the platform token in the final user-agent string. Tokens added to the Post-Platform key appear after the platform token in the final user-agent string. Multiple tokens in either the Pre-Platform key or Post-Platform key are displayed in an unpredictable order.