Android Http User Agent

Android HTTP User Agent

If you don't want to call setHeader() for every request you create you can set the http client parameter CoreProtocolPNames.USER_AGENT. After doing this HTTP client will automatically add this header parameter to each request.

Something like:

client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Custom user agent");

when you create your HttpClient.

User Agent in http client Android

You don't need to pass any User-Agent header, unless you're using a service that explicitly requires it such as for tracking or debugging purposes etc.

HTTP works perfectly well without the User-Agent header being present.

In HTTP user agent header of Android, what does the U mean?

U for strong security..................

http://www.useragentstring.com/Android%20Webkit%20Browser_id_18032.php

Security values:

  • N for no security

  • U for strong security

  • I for weak security

how to get the default HTTP USER AGENT from the android device?

Edit: See Prakash's answer, which is better for 2.1+.

Try http://developer.android.com/reference/android/webkit/WebSettings.html#getUserAgentString

Note that this User Agent will only apply for the embedded WebKit browser that's used by default in Android. Unfortunately, you'll need to create a new WebView object to get the user agent. Fortunately, the user agent doesn't change often, so you should only need to run this code once in your application lifetime (unless don't care about performance). Just do:

String userAgent = new WebView(this).getSettings().getUserAgentString();

Alternatively, you can use the JavaScript method navigator.getUserAgent().

Relevance of User Agent in HTTP Requests

iOS already specifies the User Agent by default. The user agent sent with NSURLConnection on iOS is

User-Agent: <app identifier>/<app version> CFNetwork/609.1.4 Darwin/12.4.0

where <app identifier> is your bundle ID (e.g. com.company.app) and <app version> is the current version of your app. Apparently, as you experience, Android does not set a default User Agent with (non-browser) HTTP requests.

The server can use this information to adapt its response to the client's needs; e.g. showing a mobile version of a webpage instead of a desktop version, or just don't return anything (to prevent simple screen scraping).

android user agent

After much research, I figured it out. There is a way to set a user agent for Android WebView.

webview.getSettings().setUserAgentString("user-agent-string");

http://developer.android.com/reference/android/webkit/WebSettings.html

Android Generic User Agent (UA)

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.



Related Topics



Leave a reply



Submit