Skip to content

Jsoup set referrer example

Jsoup set referrer example shows how to set Jsoup referrer. The example also shows Jsoup’s default referrer as well as how to set the referrer of your choice.

What is the Jsoup default referrer?

Jsoup uses an empty referrer header while connecting to the URL requested. This can be verified by running the below given code.

Output

Note: You can check your referrer by visiting the https://www.whatismyreferer.com website.

How to set the Jsoup referrer (referer) header?

Some of the websites do not work if the “referer” header is not sent along with the request. You may get HTTP 403 – Forbidden error while accessing these websites using the Jsoup. It is because the Jsoup passes an empty value for the “referer” header by default.

To set referrer or “referer” header, you can use the referrer method of the Connection class.

This method sets the HTTP referer header of the request.

Below given example shows how to set Jsoup referer to “http://www.google.com”.

Output

Try setting the referer header as “https://www.google.com” if you are getting the forbidden error. Plus, you should also always set the Jsoup user-agent header when connecting to any website.

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

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

About the author

Leave a Reply

Your email address will not be published.