Setting user agent in Java httpclient and allow redirects to true

I am trying to set my user agent string in the HttpClient apache object in Java but I cannot find out how to do it.

Please help!

  • Android rotate bitmap 90 degrees results in squashed image. Need a true rotate between portrait and landscape
  • WebView Crash by java.io.IOException: close failed: EIO (I/O error) libcore.io.IoUtils.close(IoUtils.java:41)
  • Android Studio debugger highlights the wrong lines
  • Https Connection Android
  • When To Unregister Broadcast Receiver registered in onCreate?
  • why use Retrofit when we have OkHttp
  • Also I am trying to enable redirects to true but also cannot find this option within the HttpClient object.

    Thanks

    Andy

  • Android run a Task Periodically
  • RuntimeException: Error inflating class fragment
  • Android append text file
  • Android Sqlite IN, NOT IN syntax
  • last block incomplete with CipherInputStream/CipherOutputStream, even with padding AES/CBC/PKCS5Padding
  • securing a REST API accessible from Android
  • 3 Solutions collect form web for “Setting user agent in Java httpclient and allow redirects to true”

    HttpClient httpclient = new HttpClient();
    httpclient.getParams().setParameter(
        HttpMethodParams.USER_AGENT,
        "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2"
    );
    

    With HttpClient 4.0, the following worked for me:

    import org.apache.http.params.HttpProtocolParams;
    
    HttpClient httpclient = new HttpClient();
    HttpProtocolParams.setUserAgent(httpclient.getParams(), "My fancy UA");
    

    HttpProtocolParams resides in the httpcore JAR file: http://hc.apache.org/httpcomponents-core/download.html

    Use AndroidHttpClient, and pass the user agent as a parameter to newInstance:

    AndroidHttpClient client = AndroidHttpClient.newInstance(String userAgent);
    

    There are other good reasons to use AndroidHttpClient instead of the raw HttpClient as well.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.