Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android HTTP User Agent

How do I get the real device in http_user_agent? When I use a WebView, I can get the real value like this:

[HTTP_USER_AGENT] => Mozilla/5.0(Linux; U; Android 2.2; en-gb; LG-P500 Build/FRF91) 
AppleWebKit/533.0 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1

But when I use an Apache connection, the result is different:

[HTTP_USER_AGENT] => Apache-HttpClient/UNAVAILABLE(java 1.4).

What's the problem?

like image 453
user430926 Avatar asked Oct 11 '10 08:10

user430926


People also ask

What is HTTP user agent?

The HTTP headers User-Agent is a request header that allows a characteristic string that allows network protocol peers to identify the Operating System and Browser of the web-server. Your browser sends the user agent to every website you connect to.

What is the Android user agent?

Published: 15 May 2022. 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.

What is AppleWebKit Android?

AppleWebKit/537.36 indicates what browser rendering engine is used. A rendering engine is what transforms HTML into an interactive webpage on the user's screen. The WebKit browser engine was developed by Apple and is primarily used by Safari, Chromium, and all other WebKit-based browsers.

How do I change my browser user agent on Android?

Switch to the “Advanced” tab in the top-right corner, then tap “User agent” at the top of the “Customize” sub-section. Tap “User agent” at the top of the “Customize” sub-section of the “Advanced” tab. Select one of the four built-in user agents or tap “Custom” and enter your own value, then tap “OK” to save.


3 Answers

To complete the accepted answer, if you want the default user agent use System.getProperty("http.agent")

client.getParams().setParameter(CoreProtocolPNames.USER_AGENT,
                                System.getProperty("http.agent"));
like image 173
Maragues Avatar answered Oct 16 '22 10:10

Maragues


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.

like image 60
Nic Strong Avatar answered Oct 16 '22 10:10

Nic Strong


If you want to set your own user agent header then you have to use the setHeader method.

In case of a HTTP Post request you just set it like this.

private String url = "http://myfancyurl.com/";
private String ua = "My Custom UA Header String";

private HttpPost post = new HttpPost(url);
post.setHeader("User-Agent", ua);

This was just a short explanation how to set a custom user agent string. Your code might look different. The important part is the setHeader method.

like image 13
Octavian A. Damiean Avatar answered Oct 16 '22 10:10

Octavian A. Damiean