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.

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

 

Tags
Join 1000+ fellow learners! Enter your email address below: