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

18,948

Solution 1

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" );

Solution 2

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.

Share:
18,948

Related videos on Youtube

cottonBallPaws
Author by

cottonBallPaws

Updated on April 06, 2020

Comments

  • cottonBallPaws
    cottonBallPaws about 4 years

    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.

Related