External Libraries

Jsoup set user agent example

Jsoup set user agent example shows how to set Jsoup user agent in Java. Example also shows default Jsoup user agent as well as how to set Jsoup user agent to Google Chrome, Firefox or any other browser.

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 below given code.

Output

Your output could be different according to the Java version you are using.

How to set Jsoup user agent?

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

This method sets the “user-agent” header of the request.

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

Output

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

You should also set the referrer and connection timeout values along with the user agent string. If you are behind proxy server, you should set the proxy in Jsoup as well. Visit Jsoup examples to learn more about the library. Please let us know your views in the comments section below.

Want to learn quickly?
Try one of the many quizzes. I promise you will not be disappointed.

Tags

About the author

rahimv

rahimv

rahimv has over 15 years of experience in designing and developing Java applications. His areas of expertise are J2EE and eCommerce. If you like the website, follow him on Facebook, Twitter or Google Plus.

Leave a Reply

avatar
newest oldest most voted
Yoann
Yoann

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 ?