Skip to content

Jsoup set user agent example

Jsoup set user agent example shows how to set Jsoup user agent in Java. The example also shows the default Jsoup user agent as well as how to set Jsoup user agent to Google Chrome, Firefox or any other browser of your choice.

What is the default Jsoup user agent?

When you connect to any URL or website, Jsoup uses the Java version of your computer as a default user agent string. That is because, under the hood, Jsoup uses built-in Java classes Connection/HTTPURLConnection to connect to the URL. You can verify the Jsoup default user agent by running the below-given code.

Output

Your output could be different according to the Java and Jsoup versions you are using.

How to set the user-agent header for Jsoup?

You can use the userAgent method of the Jsoup Connection class to set the user agent of your choice.

This method sets the “user-agent” header of the request to the value specified as the method argument.

You can verify your browser’s user agent string by visiting http://www.useragentstring.com. The below given example sets the user agent to Chrome 41.0.2228.0.

Output

You can also set the user agent to mimic a mobile device to request the mobile version of the website. You can do so by setting the user agent to Android, iPhone, Windows, or any other mobile device user agent. You can get a complete list of the user agent string from here.

You should always set the referrer and connection timeout values along with the user agent string. If you are behind a proxy server, you should set the proxy in Jsoup as well. It becomes necessary if your requests are being blocked by the webservers.

This example is a part of the Jsoup tutorial with examples.

Please let us know your views in the comments section below.

About the author

1 comments

  1. Hi !
    So you set the proxy for the whole connections from the JVM.
    Do you have an idea to set the proxy for this Jsoup.connect() only ?

Leave a Reply

Your email address will not be published.