开发者

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:

开发者_JS百科[HTTP_USER_AGENT] => Apache-HttpClient/UNAVAILABLE(java 1.4).

What's the problem?


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.


You can read useragent using webview:

new WebView(this).getSettings().getUserAgentString();

Or using system property:

System.getProperty("http.agent")

Source: https://stackoverflow.com/a/50610164/3317927

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜