Android HTTP User Agent

后端 未结 4 1620
生来不讨喜
生来不讨喜 2020-11-27 13:58

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; And         


        
相关标签:
4条回答
  • 2020-11-27 14:05

    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.

    0 讨论(0)
  • 2020-11-27 14:06

    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 讨论(0)
  • 2020-11-27 14:15

    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.

    0 讨论(0)
  • 2020-11-27 14:23

    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"));
    
    0 讨论(0)
提交回复
热议问题