How To Set Fake User Agent

Java

How to set fake user agent

What is user agent?
User agent is software agent that is acting on behalf of a user. For example, an email reader is a mail user agent, and in the Session Initiation Protocol (SIP), the term user agent refers to both end points of a communications session.

User agent acts as a client in a network protocol used in communications within a client–server distributed computing system. In particular, the Hypertext Transfer Protocol (HTTP) identifies the client software originating the request, using a “User-Agent” header, even when the client is not operated by a user.

To break it down more simply, let’s describe how the process works. When web users visit a web site a text string (a computer programming sequence of symbols) is sent to the server of that web site in order to identify the user agent. It is a request of sorts: the web browser is requesting access to the website. Part of this request, then, is that the web browser be identified as the user-agent. This identification of the user agent contains specific information, including the application name and version, the host operating system and language. An example of this is as follows:

Mozilla/5.001 (windows; U; NT4.0; en-us) Gecko/25250101

How to get the User Agent?

We can get the User Agent of a particular request through HttpServletRequest.

public String getUserAgent(HttpServletRequest request){
	String userAgent =  request.getHeader("user-agent");
	System.out.println("User Agent : " + userAgent);
	return userAgent;
}

How to set a fake User Agent?
Below we have described a way to spoof user agent in java.

package com.jkoder.http;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
 
public class SpoofUserAgent {
 
	public static void main(String[] args) throws Exception {
 
		HttpClient client = new HttpClient();
		GetMethod method = new GetMethod("jkoder.com");
		
		//custom user agent set
		method.setHeader("user-agent", "my custom googlebot");

		// Provide custom retry handler is necessary
		method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));

		try {
			// Execute the method.
			int statusCode = client.executeMethod(method);
			//If status code is not 200.
			if (statusCode != HttpStatus.SC_OK) {
				System.err.println("Method failed: " + method.getStatusLine());
			}

			// Read the response body.
			byte[] responseBody = method.getResponseBody();
			System.out.println(new String(responseBody));

		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			method.releaseConnection();
		}
	}
}