除了 WebView.getSettings().getUserAgentString() 之外,还有其他方法可以获取默认用户代理字符串吗?

发布于 2024-09-30 22:20:48 字数 73 浏览 10 评论 0原文

我想在 HttpClient 连接中使用手机的默认用户代理,并且想知道是否有一种方法无需 WebView 即可查询即可获取用户代理。

I want to use the default user agent for the phone in a HttpClient connection and would like to know if there is a way to obtain the user agent without having to have a WebView to query.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

掩耳倾听 2024-10-07 22:20:48

对于其他可能正在寻找这个问题的人来说,答案很晚了。

我正在寻找一种方法来获取 HttpUrlConnection 使用的用户代理字符串,将其与 HttpClient 一起使用并用我自己的版本信息修改它。这样,我的 Android 应用程序提供了一些有用的版本信息,我可以从服务器的日志文件中提取这些信息(Android 版本、设备名称/类型以及应用程序的版本)。

例如,使用 HttpUrlConnection 时我手机的用户代理字符串如下所示:

Dalvik/1.4.0 (Linux; U; Android 2.3.5; HTC Desire HD A9191 Build/GRJ90)

该字符串可以从系统属性中获取,如下所示:

String userAgent = System.getProperty( "http.agent" );

Very late answer, for others that may be looking for this.

I was looking for a way to obtain the user agent string used by HttpUrlConnection, to use it with HttpClient and amend it with my own version info. This way, my Android app provides some useful version info I can extract from the server's log files (Android Version, device name/type, and the version of my app).

For example, the user agent string for my phone when using HttpUrlConnection looks like this:

Dalvik/1.4.0 (Linux; U; Android 2.3.5; HTC Desire HD A9191 Build/GRJ90)

This string can be obtained from system properties like so:

String userAgent = System.getProperty( "http.agent" );
等风来 2024-10-07 22:20:48

从 API 级别 17 开始,WebSettings 中有一个静态方法,它返回 WebView 使用的默认用户代理字符串:

WebSettings.getDefaultUserAgent(context)

由于该方法是静态的,因此您不需要WebView 实例来运行它。

Starting from API level 17 there is a static method in WebSettings which returns the default User-Agent string used by a WebView:

WebSettings.getDefaultUserAgent(context)

Since the method is static you don't need a WebView instance to run it.

娇俏 2024-10-07 22:20:48

不,这是获取用户代理字符串的唯一官方方法。

顺便说一句,返回的字符串是这样的:

Mozilla/5.0 (Linux; U; Android 1.1; en-gb; dream) AppleWebKit/525.10+ (KHTML, like Gecko) Version/3.0.4 Mobile Safari/523.12.2 – G1 Phone

Http 服务器使用用户代理字符串来提供特定于浏览器的文档版本。仅当您想要某个页面的移动版本和/或您想稍后在 Android 浏览器中显示该页面时,这才有意义。

No, this is afaik the only official way to get user agent string.

Btw, the string returned is something like this:

Mozilla/5.0 (Linux; U; Android 1.1; en-gb; dream) AppleWebKit/525.10+ (KHTML, like Gecko) Version/3.0.4 Mobile Safari/523.12.2 – G1 Phone

Http servers use user agent string to serve browser-specific versions of documents. This would only make sense if you want a mobile version of some page and/or if you want to display this later in Android's browser.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文