Android 通用用户代理 (UA)

发布于 2024-11-01 15:06:36 字数 755 浏览 0 评论 0原文

我正在构建一个 Android 应用程序来显示来自服务器的内容源。服务器是一个移动网站(例如 http://m.google.com),用于跟踪来自各种移动设备的流量客户。为了区分 Android 客户端,如何为我的应用程序提供通用字符串?

这就是我问这个问题的原因:

我得到的一些 Android 设备具有 UA 字符串,例如:

Mozilla/5.0(Linux;U;Android 2.2.1;en-us;ADR6400L 4G Build/FRG83D)AppleWebKit/533.1(KHTML,如 Gecko)版本/4.0 Mobile Safari/533.1

Mozilla/5.0 ( Linux;Android 2.1; AppleWebKit/520.17(KHTML,如 Gecko)版本/4.0 Mobile Safari/520.17

我需要将一个字符串附加到 UserAgent 字符串来标识我的应用程序。例如:

我需要做这样的事情: Mozilla/5.0(Linux;U;Android 2.1;en-us;Eclair_SPR Build/30201)AppleWebKit/520.17(KHTML,如 Gecko)版本/4.0 Mobile Safari/520.17 Android_MyFirstApp

这是正确的方法吗?

I am building an Android app to display content feed from a server. The server is a mobile website (like http://m.google.com) which tracks the traffic from various mobile clients. To differentiate an Android client, how do I provide a generic string for my app?

Here's why I ask that:

Some of the Android devices I got have UA strings like:


Mozilla/5.0 (Linux; U; Android 2.2.1; en-us; ADR6400L 4G Build/FRG83D) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1

Mozilla/5.0 (Linux; U; Android 2.1; en-us; Eclair_SPR Build/30201) AppleWebKit/520.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/520.17

I need to append a string to the UserAgent string to identify my app. For eg:

I need to do something like this:
Mozilla/5.0 (Linux; U; Android 2.1; en-us; Eclair_SPR Build/30201) AppleWebKit/520.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/520.17 Android_MyFirstApp.

Is this the correct way to do it?

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

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

发布评论

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

评论(5

任谁 2024-11-08 15:06:37

既然您控制了 Android 客户端,为什么不创建一个通用标头字符串,并在每次应用程序调用服务器时将其设置在标头中呢?这样您就可以确保字符串是唯一的,并且还可以添加任何其他有用的信息发送到服务器。您应该能够使用 webView.loadUrl() 来设置额外的标头。

Since you control your Android client, why don't you create a generic header string, and set it in header every time your app makes a server call? That way you can ensure the string is unique, and can also add any other useful info to be sent to server. You should be able to use webView.loadUrl() to set extra headers.

绳情 2024-11-08 15:06:37

您完全可以做到这一点,并且在developer.android.com上,当他们谈论WebView时,也建议这样做,特别是如果您想为您的Web视图构建一个Web应用程序。参考此处: http://developer.android.com/guide/webapps/webview.html

我建议不仅要在用户代理中保留对应用程序的引用,还要跟踪版本。

不管怎样,我也想改变我的用户代理,这里的讨论也鼓励我这样做。

这是我的实现:

在您的 Android 应用程序上:

String versionName="";
int versionCode=0;
try {
    versionName = getBaseContext().getPackageManager().getPackageInfo(getBaseContext().getPackageName(), 0 ).versionName;
    versionCode = getBaseContext().getPackageManager().getPackageInfo(getBaseContext().getPackageName(), 0 ).versionCode;
} catch (NameNotFoundException e) {
    e.printStackTrace();
}

如果您想自动递增内部版本号,又名。 versionCode 您也可以查看另一篇 Stack Overflow 帖子,即 C# 解决方案。

之后您只需更改用户代理即可。

WebView mywebview = (WebView)findViewById(R.id.webView);
String myUserAgent = " MyFancyAPPName  V."+versionName+"."+versionCode;

mywebview.getSettings().setUserAgentString(mywebview.getSettings().getUserAgentString()+myUserAgent);

在您的 Web 应用程序上:在 PHP 中

<?php
$ua = strtolower($_SERVER['HTTP_USER_AGENT']);

if(stripos($ua,'MyFancyAPPName') !== false){
    //do whatever you wish here
}
?>

或在 JavaScript 中

var ua = navigator.userAgent.toLowerCase();
var isAndroid = ua.indexOf("MyFancyAPPName") > -1;
if(isAndroid) { 
    //do whatever you wish here
}

或者您可以直接从 .htaccess 文件中检测它:

RewriteCond %{HTTP_USER_AGENT} ^.*MyFancyAPPName.*$
RewriteRule ^(.*)$ http://www.MyWebSite/MyFancyAPPName [R=301]

You can totally do that and at developer.android.com suggest it as well, when they talk about the WebView, especially if you want to build a web app for your web view. Reference here: http://developer.android.com/guide/webapps/webview.html

Id suggest not only to keep reference to the application in your User Agent but to also keep track of the version as well.

Anyways, I was looking to change my UA too and the discussions here and the encouraged me to do so as well.

Here is my implementation:

On your Android APP:

String versionName="";
int versionCode=0;
try {
    versionName = getBaseContext().getPackageManager().getPackageInfo(getBaseContext().getPackageName(), 0 ).versionName;
    versionCode = getBaseContext().getPackageManager().getPackageInfo(getBaseContext().getPackageName(), 0 ).versionCode;
} catch (NameNotFoundException e) {
    e.printStackTrace();
}

If you want to auto increment the build number aka. versionCode you may take a look to this other Stack Overflow post as well, the C# solution.

Afterwards you just change the User Agent.

WebView mywebview = (WebView)findViewById(R.id.webView);
String myUserAgent = " MyFancyAPPName  V."+versionName+"."+versionCode;

mywebview.getSettings().setUserAgentString(mywebview.getSettings().getUserAgentString()+myUserAgent);

On your Web Application: In PHP

<?php
$ua = strtolower($_SERVER['HTTP_USER_AGENT']);

if(stripos($ua,'MyFancyAPPName') !== false){
    //do whatever you wish here
}
?>

Or In JavaScript

var ua = navigator.userAgent.toLowerCase();
var isAndroid = ua.indexOf("MyFancyAPPName") > -1;
if(isAndroid) { 
    //do whatever you wish here
}

Or you can directly detect it from the .htaccess file:

RewriteCond %{HTTP_USER_AGENT} ^.*MyFancyAPPName.*$
RewriteRule ^(.*)$ http://www.MyWebSite/MyFancyAPPName [R=301]
再浓的妆也掩不了殇 2024-11-08 15:06:37

当您使用 Web 视图访问用户代理时,请确保

运行new WebView(this).getSettings().getUserAgentString();

在 UI 线程上

。如果你想在后台线程中访问用户代理。使用

System.getProperty("http.agent")

要检查用户代理是否有效,请使用此 https://deviceatlas.com/device-data/user-agent-tester

When you use the web view to access the user-agent, make sure you run the

new WebView(this).getSettings().getUserAgentString();

on the UI thread.

If you want to access the user agent in the background thread. use

System.getProperty("http.agent")

To check whether a user-agent is valid or not use this https://deviceatlas.com/device-data/user-agent-tester

苦妄 2024-11-08 15:06:37

这取决于您使用什么框架来提出请求。如果您使用的是 org.apache 类,则可以在 HttpMessage< 上调用 setHeader('User-Agent', "Generic user agent here") /code> 您用来执行您的请求。

It depends on what framework you're using to make your requests. If you're using the org.apache classes, you can call setHeader('User-Agent', "Generic user agent here") on the HttpMessage you use to do your request.

眼眸 2024-11-08 15:06:36

要更改用户代理,您需要在 HTTP 请求中发送自定义 User-Agent: 标头。假设您使用 Android org.apache.http.client.HttpClient 类,您有两个选择:

  1. 在每个请求上设置用户代理标头。您可以通过在创建 HttpRequest(HttpPost、HttpGet 等)对象后调用 setHeader() 来执行此操作:
HttpGet get = new HttpGet(url);
get.setHeader("User-Agent", myUserAgent);
  1. 更改默认的用户代理参数,这将影响该 HttpClient 类的所有未来实例。您可以通过使用 getParams() 从客户端读取 HttpParams 集合,然后使用 setParameter() 更新用户代理来完成此操作:
DefaultHttpClient http = new DefaultHttpClient(); 
http.getParams().setParameter(CoreProtocolPNames.USER_AGENT, myUserAgent);

如果您想追加而不是替换用户代理,您可以先读取现有的用户代理,更改它,然后设置使用上述方法之一返回。

编辑:

既然您说您正在使用 WebView 视图,您将需要在那里使用 WebSettings 自定义点。基本上是相同的过程。在调用任何 load() 方法(loadUrl、loadData 等)之前,您需要设置用户代理。只要 WebView 实例存在,更改后的用户代理就会持续存在,因此您可以在 Activity 的 onCreate() 中执行此操作:

view = (WebView)findViewById(R.id.webview);
view.getSettings().setUserAgentString(myUserAgent);

同样,如果您想追加而不是替换,请使用 getUserAgentString() 来读取,然后更新它并再次设置回来。

To change the user agent you need to send a custom User-Agent: header with your HTTP request. Assuming you're using the Android org.apache.http.client.HttpClient class, you have two options:

  1. Set the user agent header on each request. You do this by calling setHeader() on your HttpRequest (HttpPost, HttpGet, whatever) object after you create it:
HttpGet get = new HttpGet(url);
get.setHeader("User-Agent", myUserAgent);
  1. Change the default User Agent parameter, which will affect all future instances of that HttpClient class. You do this by reading the HttpParams collection from your client with getParams() then updating the user agent using setParameter():
DefaultHttpClient http = new DefaultHttpClient(); 
http.getParams().setParameter(CoreProtocolPNames.USER_AGENT, myUserAgent);

If you want to append instead of replace the user agent, you can read the existing one first, change it, and set it back using either of the above methods.

EDIT:

Since you said you're using the WebView view you will need to use the WebSettings customization point there. It's basically the same process. Before you call whichever load() method (loadUrl, loadData, etc) you do set the user agent. The changed user-agent will persist as long as that instance of the WebView is around, so you'd do this in the onCreate() of your Activity:

view = (WebView)findViewById(R.id.webview);
view.getSettings().setUserAgentString(myUserAgent);

Again, if you want to append instead of replace, use getUserAgentString() to read it, then update it and set it back again.

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