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

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

RahimV

RahimV

My name is RahimV and I have over 16 years of experience in designing and developing Java applications. Over the years I have worked with many fortune 500 companies as an eCommerce Architect. My goal is to provide high quality but simple to understand Java tutorials and examples for free. If you like my website, follow me on Facebook and Twitter.

Add Comment

Your email address will not be published. Required fields are marked *

Online Shopping