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.
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.
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.
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.
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" );
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With