android 浏览器开发 useragent,[Android] android系统浏览器User-Agent的问题

bart.png

[This post is by Bart Sears, who manages the Android Browser team. —Tim Bray]

This posting describes some issues when browsing websites with mobile variants using large-form-factor Android devices. This posting will be of interest both to OEMs (with recommendations on how to set the User Agent string for the device) and to web site designers/administrators (with recommendations on how to decide to provide either a mobile version, a desktop version, or a large-form-factor touch device version of the site).

Details

With the advent of Android devices with larger form factors, we’ve been evaluating the best way for web sites to provide a UI appropriate for the various Android devices that are now available to consumers. We have received feedback that consumers using larger-form-factor devices often prefer the “full” or “desktop” version of the site over the “mobile” version. Most websites providing “mobile” versions key off of the HTTP User-Agent header field to determine whether to provide the full site or a mobile version.

While large-form-factor Android devices could use “User Agent Spoofing” to provide a desktop User Agent in the HTTP header, we recommend against this. There may be site customizations needed for Android devices (for example changes in the way that mouseover is used) and the site would be unable to provide these customizations if it receives a spoofed User Agent that did not indicate that this was an Android device.

Currently, Android devices provide the following (in addition to standard info) in the User-Agent: "Android", a version number, a device name, a specific build, Webkit version info, and "Mobile". For example, Froyo on a Nexus One has the following User Agent:

Mozilla/5.0 (Linux; U; Android 2.2.1; en-us; Nexus One Build/FRG83) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1

The "Mobile" string in the User Agent indicates that this device would prefer a version of the website optimized for Mobile (small form factor devices), if available.

We recommend that manufactures of large-form-factor devices (where the user may prefer the standard web site over a mobile optimized version) remove "Mobile" from the User Agent (and keep the rest of the User Agent as currently implemented). Web sites can then key off "Mobile" in the User Agent to decide on which UI version to present to the device. So a large screen device running Froyo would have a User Agent similar to:

Mozilla/5.0 (Linux; U; Android 2.2.1; en-us; device Build/FRG83) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Safari/533.1

Where "device" would be replaced with the actual name of the new device. Sites can continue to use “Android” in the User Agent to optimize for Android specific features and can also key off of “Mobile” to determine which UI to present.

本人补充相关信息如下:

android系统(webview/webkit)处理user agent的地方在文件frameworks/base/core/java/android/webkit/WebSettings.java里的函数getCurrentUserAgent中,使用的格式化字符串是web_user_agent,定义在文件frameworks/base/core/res/res/values/strings.xml中,同时WebSettings也提供setUserAgentString来自定义user agent.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android UserAgent 批量生成指的是通过程序一次性生成多个 Android 设备的 UserAgent。UserAgent 是一个 HTTP 请求的头部信息,用于标识浏览器或客户端的身份。生成不同的 UserAgent 可以用于模拟不同的 Android 设备,测试或爬虫等用途。 要批量生成 Android UserAgent,可以使用编程语言(如 Python、Java 等)编写一个脚本,通过循环生成不同的 UserAgent。生成 UserAgent 时需要考虑以下几点: 1. Android 版本:可以选择不同的 Android 版本,如 Android 4.4、Android 5.0、Android 6.0 等。 2. 设备型号:可以选择不同的设备型号,如 Samsung Galaxy S10、Google Pixel 3、OnePlus 7 等。 3. 浏览器:可以选择不同的浏览器,如 Chrome、Firefox、Opera 等。 4. 随机化:可以对上述参数进行随机化,以模拟更多不同的设备和浏览器组合。 以下是一个使用 Python 生成 Android UserAgent 的简单示例: ``` import random android_versions = ["Android 4.4", "Android 5.0", "Android 6.0"] device_models = ["Samsung Galaxy S10", "Google Pixel 3", "OnePlus 7"] browsers = ["Chrome", "Firefox", "Opera"] user_agents = [] # 循环生成 UserAgent for _ in range(10): # 生成 10 个 UserAgent android_version = random.choice(android_versions) device_model = random.choice(device_models) browser = random.choice(browsers) user_agent = f"Mozilla/5.0 ({android_version}; {device_model}) AppleWebKit/537.36 (KHTML, like Gecko) " \ f"{browser}/73.0.3683.75 Mobile Safari/537.36" user_agents.append(user_agent) # 打印生成的 UserAgent for user_agent in user_agents: print(user_agent) ``` 该示例生成了 10 个随机的 Android UserAgent,并输出到控制台。可以根据需要调整生成的数量、可选参数等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值