Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to obtain the default user agent string aside from WebView.getSettings().getUserAgentString()?

Tags:

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.

like image 937
cottonBallPaws Avatar asked Nov 05 '10 06:11

cottonBallPaws


People also ask

Where is the user agent string?

The User-Agent (UA) string is contained in the HTTP headers and is intended to identify devices requesting online content. The User-Agent tells the server what the visiting device is (among many other things) and this information can be used to determine what content to return.

How do I change user agent in Android WebView?

To set user-agent string of WebView, call its getSettings(). setUserAgentString(ua); where ua is is your user-agent string. remark: to check what user-agent your browser set, browse to http://whatsmyuseragent.com/, allows you to view details about your user agent.

What is a browser user agent string?

A browser's User-Agent string (UA) helps identify which browser is being used, what version, and on which operating system. When feature detection APIs are not available, use the UA to customize behavior or content to specific browser versions.


2 Answers

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" );
like image 98
Stefan Frye Avatar answered Oct 03 '22 03:10

Stefan Frye


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.

like image 32
Idolon Avatar answered Oct 03 '22 05:10

Idolon