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