网页开发测试的随机用户代理生成器
生成真实的浏览器用户代理字符串,具有按设备类型、浏览器系列和操作系统过滤的选项。非常适合网页开发测试和兼容性检查。
用户代理生成器
检测到的用户代理
关于用户代理字符串
用户代理是一个字符串,网页浏览器和其他应用程序用来向网络服务器识别自己。
它通常包含有关浏览器、操作系统、设备和其他客户端详细信息的信息,帮助网站提供优化的内容。
文档
用户代理生成器
介绍
用户代理字符串是网页浏览器和其他应用程序发送给网站的特定文本标识符,用于识别它们自己。这个字符串通常包含有关所使用的浏览器、操作系统、设备类型和渲染引擎的信息。对于网页开发者、测试人员和好奇的用户来说,访问当前的用户代理字符串对于故障排除和开发目的非常有价值。
这个用户代理生成器工具自动检测并显示您当前浏览器的用户代理字符串,而无需任何手动选择或配置。该工具提供了一个干净、简单的界面,可以显示您检测到的用户代理,并提供一键复制选项,方便分享或在项目中使用。如果您更改浏览器或需要刷新检测(例如,在更改用户代理设置后),一个方便的刷新按钮可以让您立即更新显示的信息。
了解您当前的用户代理可以帮助您识别网站如何感知您的浏览器和设备,这在调试网站行为或测试Web应用程序时尤其有用。
用户代理结构
用户代理字符串遵循特定的模式,具体取决于浏览器和平台,但通常包含几个常见组件:
- 浏览器标识符:通常以“Mozilla/5.0”开头,出于历史兼容性原因
- 平台/操作系统信息:有关操作系统(Windows、macOS、Android、iOS)的详细信息
- 浏览器引擎:渲染引擎(如Gecko、WebKit或Blink)
- 浏览器详细信息:特定的浏览器名称和版本
以下是主要浏览器的典型用户代理结构的细分:
Chrome
1Mozilla/5.0 (platform; details) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/version Safari/537.36
2
Firefox
1Mozilla/5.0 (platform; rv:geckoversion) Gecko/geckotrail Firefox/firefoxversion
2
Safari
1Mozilla/5.0 (platform) AppleWebKit/webkitversion (KHTML, like Gecko) Version/safariversion Safari/safariversion
2
Edge
1Mozilla/5.0 (platform) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/chromiumversion Safari/537.36 Edg/edgeversion
2
平台部分在桌面和移动设备之间差异显著:
桌面示例:
- Windows:
Windows NT 10.0; Win64; x64
- macOS:
Macintosh; Intel Mac OS X 10_15_7
- Linux:
X11; Linux x86_64
移动示例:
- Android:
Linux; Android 12; SM-G998B
- iOS:
iPhone; CPU iPhone OS 15_4 like Mac OS X
设备类型差异
桌面用户代理
桌面用户代理通常包含特定的操作系统信息、架构细节(如x86_64或Win64),有时还包含语言偏好。与移动用户代理相比,它们在不同浏览器之间通常更为一致。
移动用户代理
移动用户代理包含设备型号信息、移动操作系统版本,并且通常在末尾包含“Mobile”一词。iOS设备上的移动Safari将包含“iPhone”或“iPad”标识符,而Android设备则包含制造商和型号。
浏览器版本模式
每个浏览器遵循不同的版本模式:
- Chrome:使用四部分版本号(例如96.0.4664.110)
- Firefox:通常使用两或三部分版本号(例如95.0或95.0.2)
- Safari:使用简单的版本号,如15.2
- Edge:使用类似于Chrome的版本号,但有自己的Edge版本(例如96.0.1054.62)
用例
自动用户代理检测有几个重要的应用:
-
调试浏览器问题:快速识别您确切的浏览器配置,以便向网页开发者或支持团队报告问题。
-
API开发:测试您当前的浏览器如何向使用用户代理信息进行客户端检测的API进行识别。
-
跨浏览器测试:在不同浏览器之间测试网站时,验证您的浏览器发送的用户代理字符串。
-
隐私意识:了解您的浏览器与您访问的网站共享了哪些信息。
-
Web开发:轻松访问您的浏览器用户代理,以便在开发环境中使用或在配置服务器端用户代理检测时使用。
-
技术支持:在排除Web应用程序问题时,与技术支持共享您确切的浏览器配置。
-
教育目的:了解浏览器用户代理字符串的结构和包含的信息。
替代方案
虽然我们的自动用户代理检测器在许多场景中很有用,但也有其他方法:
-
浏览器开发者工具:大多数现代浏览器允许您通过开发者工具查看用户代理(通常在网络选项卡下或在控制台中输入
navigator.userAgent
)。 -
我的浏览器服务:像WhatIsMyBrowser.com这样的网站提供有关您的浏览器配置的详细信息,包括用户代理。
-
用户代理API服务:访问时返回用户代理信息的在线API。
-
用户代理分析器扩展:提供用户代理组件详细分解的浏览器扩展。
-
命令行工具:使用
curl
和--user-agent
标志的工具可以用于测试用户代理检测。
每种替代方案都有其优缺点,可能更适合您的具体需求。我们的工具专注于简单性和即时访问,无需开发者工具或外部服务。
历史
用户代理字符串的概念可以追溯到万维网的早期。术语“用户代理”源于HTTP规范,它指的是向Web服务器发出请求的客户端应用程序。
早期(1990年代)
第一个广泛使用的浏览器NCSA Mosaic包含一个简单的用户代理字符串,用于识别浏览器名称和版本。当Netscape Navigator发布时,它使用了类似的格式。然而,随着Web服务器开始根据浏览器提供不同的内容,出现了一种名为“浏览器嗅探”的做法。
浏览器战争与用户代理伪装(1990年代末)
在Netscape和Internet Explorer之间的浏览器战争期间,网站通常仅向特定浏览器提供优化内容。为了确保兼容性,浏览器开始包含识别自身为其他浏览器的字符串。这就是为什么大多数现代浏览器仍在其用户代理字符串中包含“Mozilla”的原因,这是Netscape Navigator的代号。
移动革命(2000年代-2010年代)
移动设备的兴起给用户代理字符串带来了新的复杂性。移动浏览器需要将自己识别为移动设备,以接收适当的内容,导致添加设备标识符和移动特定的标记。
现代挑战(2010年代至今)
随着Web生态系统变得越来越复杂,用户代理字符串变得越来越复杂。它们现在包含对多个浏览器引擎(如“AppleWebKit”和“Gecko”)的引用,出于兼容性原因,即使这些引擎实际上并未被使用。
这种复杂性导致准确解析用户代理字符串的挑战,一些Web标准组织提议弃用或简化用户代理字符串,以支持更结构化的客户端提示。然而,出于向后兼容性原因,传统的用户代理字符串仍然是Web浏览的重要组成部分。
代码示例
以下是如何在各种编程语言中处理用户代理字符串的示例:
1// JavaScript:自动检测当前用户代理
2function getCurrentUserAgent() {
3 return navigator.userAgent;
4}
5
6// 复制文本到剪贴板的函数
7function copyToClipboard(text) {
8 const textarea = document.createElement('textarea');
9 textarea.value = text;
10 document.body.appendChild(textarea);
11 textarea.select();
12 document.execCommand('copy');
13 document.body.removeChild(textarea);
14
15 // 显示“已复制!”消息
16 const copyButton = document.getElementById('copyButton');
17 const originalText = copyButton.textContent;
18 copyButton.textContent = '已复制!';
19
20 // 2秒后重置按钮文本
21 setTimeout(() => {
22 copyButton.textContent = originalText;
23 }, 2000);
24}
25
26// 刷新用户代理显示的函数
27function refreshUserAgent() {
28 const userAgentDisplay = document.getElementById('userAgentDisplay');
29 userAgentDisplay.textContent = getCurrentUserAgent();
30}
31
32// 页面加载时初始化
33document.addEventListener('DOMContentLoaded', () => {
34 refreshUserAgent();
35
36 // 设置复制按钮
37 document.getElementById('copyButton').addEventListener('click', () => {
38 copyToClipboard(getCurrentUserAgent());
39 });
40
41 // 设置刷新按钮
42 document.getElementById('refreshButton').addEventListener('click', refreshUserAgent);
43});
44
1# Python:Flask应用程序检测并显示用户代理
2from flask import Flask, request, render_template_string
3
4app = Flask(__name__)
5
6@app.route('/')
7def detect_user_agent():
8 user_agent = request.headers.get('User-Agent')
9
10 html = '''
11 <!DOCTYPE html>
12 <html>
13 <head>
14 <title>用户代理检测器</title>
15 <style>
16 body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; }
17 .user-agent { background: #f5f5f5; padding: 15px; border-radius: 5px; font-family: monospace; word-break: break-all; }
18 button { padding: 8px 16px; margin-top: 10px; cursor: pointer; }
19 </style>
20 </head>
21 <body>
22 <h1>您的用户代理</h1>
23 <div class="user-agent">{{ user_agent }}</div>
24 <button onclick="copyToClipboard()">复制到剪贴板</button>
25 <button onclick="location.reload()">刷新</button>
26
27 <script>
28 function copyToClipboard() {
29 const userAgent = '{{ user_agent }}';
30 navigator.clipboard.writeText(userAgent)
31 .then(() => alert('已复制到剪贴板!'))
32 .catch(err => console.error('复制失败:', err));
33 }
34 </script>
35 </body>
36 </html>
37 '''
38
39 return render_template_string(html, user_agent=user_agent)
40
41if __name__ == '__main__':
42 app.run(debug=True)
43
1<?php
2// PHP:检测并显示用户代理
3function getUserAgent() {
4 return $_SERVER['HTTP_USER_AGENT'] ?? '未检测到用户代理';
5}
6
7$userAgent = getUserAgent();
8?>
9
10<!DOCTYPE html>
11<html>
12<head>
13 <title>用户代理检测器</title>
14 <style>
15 body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; }
16 .user-agent { background: #f5f5f5; padding: 15px; border-radius: 5px; font-family: monospace; word-break: break-all; }
17 button { padding: 8px 16px; margin-top: 10px; cursor: pointer; }
18 </style>
19</head>
20<body>
21 <h1>您的用户代理</h1>
22 <div class="user-agent" id="userAgentDisplay"><?php echo htmlspecialchars($userAgent); ?></div>
23
24 <button id="copyButton">复制到剪贴板</button>
25 <button id="refreshButton" onclick="window.location.reload()">刷新</button>
26
27 <script>
28 document.getElementById('copyButton').addEventListener('click', function() {
29 const userAgent = document.getElementById('userAgentDisplay').textContent;
30 navigator.clipboard.writeText(userAgent)
31 .then(() => {
32 this.textContent = '已复制!';
33 setTimeout(() => {
34 this.textContent = '复制到剪贴板';
35 }, 2000);
36 })
37 .catch(err => console.error('复制失败:', err));
38 });
39 </script>
40</body>
41</html>
42
1// Java:Servlet检测并显示用户代理
2import java.io.IOException;
3import java.io.PrintWriter;
4
5import javax.servlet.ServletException;
6import javax.servlet.annotation.WebServlet;
7import javax.servlet.http.HttpServlet;
8import javax.servlet.http.HttpServletRequest;
9import javax.servlet.http.HttpServletResponse;
10
11@WebServlet("/user-agent")
12public class UserAgentServlet extends HttpServlet {
13 private static final long serialVersionUID = 1L;
14
15 protected void doGet(HttpServletRequest request, HttpServletResponse response)
16 throws ServletException, IOException {
17
18 // 从请求中获取用户代理
19 String userAgent = request.getHeader("User-Agent");
20
21 response.setContentType("text/html");
22 PrintWriter out = response.getWriter();
23
24 out.println("<!DOCTYPE html>");
25 out.println("<html>");
26 out.println("<head>");
27 out.println("<title>用户代理检测器</title>");
28 out.println("<style>");
29 out.println("body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; }");
30 out.println(".user-agent { background: #f5f5f5; padding: 15px; border-radius: 5px; font-family: monospace; word-break: break-all; }");
31 out.println("button { padding: 8px 16px; margin-top: 10px; cursor: pointer; }");
32 out.println("</style>");
33 out.println("</head>");
34 out.println("<body>");
35 out.println("<h1>您的用户代理</h1>");
36 out.println("<div class='user-agent' id='userAgentDisplay'>" + userAgent + "</div>");
37 out.println("<button id='copyButton'>复制到剪贴板</button>");
38 out.println("<button onclick='window.location.reload()'>刷新</button>");
39 out.println("<script>");
40 out.println("document.getElementById('copyButton').addEventListener('click', function() {");
41 out.println(" const userAgent = document.getElementById('userAgentDisplay').textContent;");
42 out.println(" navigator.clipboard.writeText(userAgent)");
43 out.println(" .then(() => {");
44 out.println(" this.textContent = '已复制!';");
45 out.println(" setTimeout(() => {");
46 out.println(" this.textContent = '复制到剪贴板';");
47 out.println(" }, 2000);");
48 out.println(" })");
49 out.println(" .catch(err => console.error('复制失败:', err));");
50 out.println("});");
51 out.println("</script>");
52 out.println("</body>");
53 out.println("</html>");
54 }
55}
56
1# Ruby:Sinatra应用检测并显示用户代理
2require 'sinatra'
3
4get '/' do
5 @user_agent = request.user_agent
6
7 erb :index
8end
9
10__END__
11
12@@ index
13<!DOCTYPE html>
14<html>
15<head>
16 <title>用户代理检测器</title>
17 <style>
18 body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; }
19 .user-agent { background: #f5f5f5; padding: 15px; border-radius: 5px; font-family: monospace; word-break: break-all; }
20 button { padding: 8px 16px; margin-top: 10px; cursor: pointer; }
21 </style>
22</head>
23<body>
24 <h1>您的用户代理</h1>
25 <div class="user-agent" id="userAgentDisplay"><%= @user_agent %></div>
26
27 <button id="copyButton">复制到剪贴板</button>
28 <button onclick="window.location.reload()">刷新</button>
29
30 <script>
31 document.getElementById('copyButton').addEventListener('click', function() {
32 const userAgent = document.getElementById('userAgentDisplay').textContent;
33 navigator.clipboard.writeText(userAgent)
34 .then(() => {
35 this.textContent = '已复制!';
36 setTimeout(() => {
37 this.textContent = '复制到剪贴板';
38 }, 2000);
39 })
40 .catch(err => console.error('复制失败:', err));
41 });
42 </script>
43</body>
44</html>
45
1// C#:ASP.NET Core MVC控制器和视图检测并显示用户代理
2// 控制器:
3using Microsoft.AspNetCore.Mvc;
4
5public class UserAgentController : Controller
6{
7 public IActionResult Index()
8 {
9 ViewBag.UserAgent = Request.Headers["User-Agent"].ToString();
10 return View();
11 }
12}
13
14// 视图(Index.cshtml):
15@{
16 ViewData["Title"] = "用户代理检测器";
17}
18
19<div class="container">
20 <h1>您的用户代理</h1>
21 <div class="user-agent" id="userAgentDisplay">@ViewBag.UserAgent</div>
22
23 <button id="copyButton" class="btn btn-primary">复制到剪贴板</button>
24 <button onclick="window.location.reload()" class="btn btn-secondary">刷新</button>
25</div>
26
27@section Scripts {
28 <script>
29 document.getElementById('copyButton').addEventListener('click', function() {
30 const userAgent = document.getElementById('userAgentDisplay').textContent;
31 navigator.clipboard.writeText(userAgent)
32 .then(() => {
33 this.textContent = '已复制!';
34 setTimeout(() => {
35 this.textContent = '复制到剪贴板';
36 }, 2000);
37 })
38 .catch(err => console.error('复制失败:', err));
39 });
40 </script>
41}
42
43<style>
44 .user-agent {
45 background: #f5f5f5;
46 padding: 15px;
47 border-radius: 5px;
48 font-family: monospace;
49 word-break: break-all;
50 margin-bottom: 15px;
51 }
52 button {
53 margin-right: 10px;
54 }
55</style>
56
1// Go:HTTP服务器检测并显示用户代理
2package main
3
4import (
5 "html/template"
6 "net/http"
7)
8
9const indexHTML = `
10<!DOCTYPE html>
11<html>
12<head>
13 <title>用户代理检测器</title>
14 <style>
15 body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; }
16 .user-agent { background: #f5f5f5; padding: 15px; border-radius: 5px; font-family: monospace; word-break: break-all; }
17 button { padding: 8px 16px; margin-top: 10px; cursor: pointer; }
18 </style>
19</head>
20<body>
21 <h1>您的用户代理</h1>
22 <div class="user-agent" id="userAgentDisplay">{{.}}</div>
23
24 <button id="copyButton">复制到剪贴板</button>
25 <button onclick="window.location.reload()">刷新</button>
26
27 <script>
28 document.getElementById('copyButton').addEventListener('click', function() {
29 const userAgent = document.getElementById('userAgentDisplay').textContent;
30 navigator.clipboard.writeText(userAgent)
31 .then(() => {
32 this.textContent = '已复制!';
33 setTimeout(() => {
34 this.textContent = '复制到剪贴板';
35 }, 2000);
36 })
37 .catch(err => console.error('复制失败:', err));
38 });
39 </script>
40</body>
41</html>
42`
43
44func userAgentHandler(w http.ResponseWriter, r *http.Request) {
45 userAgent := r.Header.Get("User-Agent")
46
47 tmpl := template.Must(template.New("index").Parse(indexHTML))
48 tmpl.Execute(w, userAgent)
49}
50
51func main() {
52 http.HandleFunc("/", userAgentHandler)
53
54 println("服务器已在 http://localhost:8080 启动")
55 http.ListenAndServe(":8080", nil)
56}
57
常见用户代理模式
以下是不同浏览器和平台的一些真实用户代理字符串示例:
桌面浏览器
Windows上的Chrome:
1Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36
2
macOS上的Firefox:
1Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:95.0) Gecko/20100101 Firefox/95.0
2
macOS上的Safari:
1Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.2 Safari/605.1.15
2
Windows上的Edge:
1Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36 Edg/96.0.1054.62
2
移动浏览器
Android上的Chrome:
1Mozilla/5.0 (Linux; Android 12; SM-G998B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.104 Mobile Safari/537.36
2
iPhone上的Safari:
1Mozilla/5.0 (iPhone; CPU iPhone OS 15_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.2 Mobile/15E148 Safari/604.1
2
Android上的Firefox:
1Mozilla/5.0 (Android 12; Mobile; rv:95.0) Gecko/95.0 Firefox/95.0
2
Galaxy上的Samsung Internet:
1Mozilla/5.0 (Linux; Android 12; SM-G998B) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/16.0 Chrome/92.0.4515.166 Mobile Safari/537.36
2
参考文献
-
“用户代理。” MDN Web Docs,Mozilla,https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/User-Agent
-
“浏览器用户代理字符串。” WhatIsMyBrowser.com,https://www.whatismybrowser.com/guides/the-latest-user-agent/
-
“HTTP用户代理头解释。” KeyCDN,https://www.keycdn.com/support/user-agent
-
“客户端提示。” MDN Web Docs,Mozilla,https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Client_hints
-
“用户代理字符串历史。” WebAIM,https://webaim.org/blog/user-agent-string-history/
-
“浏览器检测使用用户代理。” Google Developers,https://developer.chrome.com/docs/multidevice/user-agent/
-
“Navigator.userAgent。” MDN Web Docs,Mozilla,https://developer.mozilla.org/zh-CN/docs/Web/API/Navigator/userAgent
反馈
点击反馈提示开始给这个工具反馈