Set a USER_AGENT in an android application, is it the same for HTTP?

1

I am trying to establish the USER_AGENT in an Android application, so that it can navigate, with which I indicate it. I am using the following lines of code:

 public void setUserAgent(){
      DefaultHttpClient http = new DefaultHttpClient();
      http.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "UserAgentPropio");
        Log.e("","");
        Log.e("USER AGENT : " + System.getProperty("http.agent"),"");
        Log.e("","");
 }

The question really branches in two:

  • First Is the USER_AGENT the same as it is used in HTTP and the one that is Applies to Android? , my question comes from that, on Android it is used to navigate in the WebView a specific User_Agent, but you can use another to browse the internet.

  • And the question I have as main.

  • When I do the System.getProperty(...) I detect another one, which I do not know if I'm detecting it in the indicated way.

    Greetings.

        
    asked by CodeNoob 02.11.2016 в 17:19
    source

    1 answer

    2

    As an important comment, Apache classes are marked as obsolete:

    import org.apache.http.impl.client.DefaultHttpClient;
    

    now it is recommended to use HttpURLConnection

    To define a User-Agent to your connection using DefaultHttpClient is:

    DefaultHttpClient http = new DefaultHttpClient(); 
    http.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "<Mi USER-AGENT>");
    

    If you use UrlConnection ( HttpURLConnection is subclass), defined this way:

    URLConnection conn = new URL("http://...").openConnection();
    conn.setRequestProperty("User-agent", "<Mi USER-AGENT>");
    conn.connect();
    
      

    First Is the USER_AGENT the same as it is used in HTTP and the one that is   Applies to Android? , my question comes from that, on Android it is used   to navigate in the WebView a specific User_Agent, but you can   use another to browse the internet.

    When you define another User-Agent you are overwriting this property and you can browse without problem unless you have configured a specific User-Agent to allow you to navigate when making the request.

      

    And the question I have as main. When I do the   System.getProperty (...) detects another one, which I do not know if   I'm detecting in the indicated way.

    Through System.getProperty("http.agent") you get the default User-Agent.

    Which you can overwrite your connection.

    http.getParams().setParameter(CoreProtocolPNames.USER_AGENT,
                                    System.getProperty("http.agent"));
    

    You must decide which one you need, the default or overwrite another one:

    http.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "<Mi USER-AGENT>");
    
        
    answered by 02.11.2016 / 17:41
    source