views:

64

answers:

2

Hi..

Can i ask how to get http_user_agent real device? When i use 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 Apache connection, the result is different like this:

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

Can anyone know what the problem?

A: 

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.

Octavian Damiean
In this case, i didn't want to set my own user agent. I want it to be automatic like i get in WebView. Is there any solution? Thanks.
Actually you shouldn't use other http clients User Agents, as some server serve different content depending on the user agent (i.e. optimized content for that browser)
Tseng
Well if you have a special case where you have a service depending on a custom user agent string then there is no way around.
Octavian Damiean
+1  A: 

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.

Nic Strong