Android、webview 用户代理与浏览器用户代理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12727117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:34:50  来源:igfitidea点击:

Android, webview user agent vs browser user agent

androidandroid-webviewuser-agent

提问by a fair player

I'm building my website and I want to know whether the user is using Android's browser or an app with a webview.

我正在构建我的网站,我想知道用户使用的是 Android 浏览器还是带有 webview 的应用程序。

is it possible ??

是否可以 ??

采纳答案by Matthew

FROM: http://googlewebmastercentral.blogspot.com/2011/03/mo-better-to-also-detect-mobile-user.html

来自:http: //googlewebmastercentral.blogspot.com/2011/03/mo-better-to-also-detect-mobile-user.html

With a User-Agent like this:

使用这样的用户代理:

Mozilla/5.0 (Linux; U; Android 3.0; en-us; Xoom Build/HRI39) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13

Mozilla/5.0 (Linux; U; Android 3.0; en-us; Xoom Build/HRI39) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13

since there is no “mobile” string, serve this user the desktop version (or a version customized for Android large-screen touch devices). The User-Agent tells us they're coming from a large-screen device, the XOOM tablet.

由于没有“移动”字符串,因此为该用户提供桌面版本(或为 Android 大屏幕触摸设备定制的版本)。User-Agent 告诉我们它们来自大屏幕设备 XOOM 平板电脑。

On the other hand, this 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

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

contains “mobile” and “android,” so serve the web surfer on this Nexus One the mobile experience!

包含“移动”和“安卓”,因此在此 Nexus One 上为网络冲浪者提供移动体验!

FROM https://stackoverflow.com/a/7327201

来自https://stackoverflow.com/a/7327201

it looks like the User-Agent is the same in webview as in the default mobile browser

看起来用户代理在 webview 中与默认移动浏览器中的相同

回答by Miguel Andres

As per Chrome dev docs: "If you're attempting to differentiate between the WebView and Chrome for Android, you should look for the presence of the Version/X.Xstring in the WebView user-agent string."

根据Chrome 开发文档:“如果您试图区分 WebView 和 Android 版 Chrome,您应该在 WebView 用户代理字符串中查找 Version/ XX字符串的存在。”

回答by jtymann

FYI: This can't be done with user agents, however it can be detected. Android's web views send an addition header "X-Requested-With". The value of this header will be the application's name space that is running the webview.

仅供参考:这不能通过用户代理完成,但是可以检测到。Android 的 Web 视图发送附加标头“X-Requested-With”。此标头的值将是运行 webview 的应用程序的名称空间。

For Example Dolphin browser sends: "mobi.mgeek.TunnyBrowser" My test app sent: "com.jamestymann.identifyawebview"

例如海豚浏览器发送:“mobi.mgeek.TunnyBrowser”我的测试应用程序发送:“com.jamestymann.identifyawebview”

The standard browser actually does not send this header at all, so it is pretty easy to detect these.

标准浏览器实际上根本不发送此标头,因此很容易检测到这些标头。

I have two caveats though:

不过我有两个警告:

  • "X-Requested-With" is a standard header and could potentially be sent from full blown webpages/browsers from desktops. (For example this is used to detect ajax calls with these values "X-Requested-With XMLHttpRequest")
  • Most google play store browsers use webviews to display webpages. Even though these are full blown browsers, they will still send this header. So if your intent is to disable this feature you may want to be careful as you may be disabling peoples default browsers.
  • “X-Requested-With”是一个标准的标头,可能是从台式机的完整网页/浏览器发送的。(例如,这用于检测具有这些值“X-Requested-With XMLHttpRequest”的ajax调用)
  • 大多数 google play 商店浏览器使用 webviews 来显示网页。即使这些是成熟的浏览器,它们仍会发送此标头。因此,如果您打算禁用此功能,您可能需要小心,因为您可能会禁用人们的默认浏览器。

回答by Paul Gregtheitroade

For more current information, look here https://developer.chrome.com/multidevice/user-agentThe lolipop and newer devices include wv)in the UserAgent.

有关更多最新信息,请查看此处https://developer.chrome.com/multidevice/user-agent棒棒糖和更新的设备包含wv)在 UserAgent 中。

回答by alireza ahmadi

You can used below code for checked android webview or browser.

您可以将以下代码用于已检查的 android webview 或浏览器。

public static bool is_mobile_and_webview()
{
     return System.Web.HttpContext.Current.Request.Browser.IsMobileDevice && System.Web.HttpContext.Current.Request.UserAgent.Contains("; wv");
}

回答by vin_mobilecem

yes and you can optimize your website by adding these meta tags

是的,您可以通过添加这些元标记来优化您的网站

<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" />
<meta name="HandheldFriendly" content="True" />

you can find more information here http://learnthemobileweb.com/2009/07/mobile-meta-tags/

你可以在这里找到更多信息http://learnthemobileweb.com/2009/07/mobile-meta-tags/

回答by MikeyKennethR

I use this serverside, to access info about client' browser (agent) in PHP

我使用这个服务器端,在 PHP 中访问有关客户端浏览器(代理)的信息

...
$_SERVER['HTTP_USER_AGENT']; // Different browsers ...
...

this boilerplate can be interpreted - hence you will know the agent ...

这个样板可以解释 - 因此你会知道代理......

on client side - navigator.userAgent

在客户端 - navigator.userAgent