Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set custom User-Agent with apache http client library 4.1?

How to make HTTPClient use custom User-Agent header?

The following code submits empty user-agent. What am I missing?

import java.io.IOException;  import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.CoreProtocolPNames; import org.apache.http.protocol.BasicHttpContext; import org.apache.http.protocol.HttpContext; import org.apache.http.util.EntityUtils;  public class TestHTTP {          public static void main(String[] args) throws ClientProtocolException, IOException {         HttpGet request = new HttpGet("http://tool.keepmeapi.com/echo");          HttpContext HTTP_CONTEXT = new BasicHttpContext();         HTTP_CONTEXT.setAttribute(CoreProtocolPNames.USER_AGENT, "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13");         request.setHeader("Referer", "http://www.google.com");          HttpClient httpClient = new DefaultHttpClient();         HttpResponse response = httpClient.execute(request, HTTP_CONTEXT);          if (response.getStatusLine().getStatusCode() < 200 || response.getStatusLine().getStatusCode() >= 400) {             throw new IOException("Got bad response, error code = " + response.getStatusLine().getStatusCode());         }          HttpEntity entity = response.getEntity();         if (entity != null) {             System.out.println(EntityUtils.toString(entity));             EntityUtils.consume(entity);         }     }  } 
like image 514
Maxim Veksler Avatar asked Feb 17 '11 09:02

Maxim Veksler


People also ask

How do I import CloseableHttpClient?

Create instance of CloseableHttpClient using helper class HttpClients . Create HttpGet or HttpPost instance based on the HTTP request type. Use addHeader method to add required headers such as User-Agent, Accept-Encoding etc. For POST, create list of NameValuePair and add all the form parameters.

What is User Agent in HTTP header?

The User-Agent request header is a characteristic string that lets servers and network peers identify the application, operating system, vendor, and/or version of the requesting user agent.

How to create HttpClient in java?

Java HttpClient GET requestHttpClient client = HttpClient. newHttpClient(); A new HttpClient is created with the newHttpClient factory method. HttpRequest request = HttpRequest.

What is CloseableHttpClient?

CloseableHttpClient is the base class of the httpclient library, the one all implementations use. Other subclasses are for the most part deprecated. The HttpClient is an interface for this class and other classes. You should then use the CloseableHttpClient in your code, and create it using the HttpClientBuilder .


2 Answers

Note: The solution is for users using the old httpcomponents 4.2 and before.

The line

request.setHeader("User-Agent", "MySuperUserAgent");

is missing. Add it and enjoy.

like image 71
AlexR Avatar answered Oct 08 '22 02:10

AlexR


Note: Starting from httpcomponents 4.3 this solution is deprecated.

You can also set a global user agent value instead of per request:

String userAgent = "NewUseAgent/1.0"; HttpClient httpClient = new DefaultHttpClient(); httpclient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, userAgent); 
like image 35
Emre Yazici Avatar answered Oct 08 '22 02:10

Emre Yazici