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.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
try{ String strText = Jsoup .connect("https://www.whatismyreferer.com/") .get() .text(); System.out.println(strText); }catch(IOException ioe){ System.out.println("Exception: " + ioe); } |
Output
1 |
What is my Referer? What is my Referer? Your HTTP referer: No referer / hidden … |
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.
1 |
Connection referrer(String httpReferrer) |
This method sets the HTTP referer header of the request.
Below given example shows how to set Jsoup referer to “http://www.google.com”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package com.javacodeexamples.libraries.jsoup; import java.io.IOException; import org.jsoup.Jsoup; public class SetJSoupReferrerExample { public static void main(String[] args) { try{ String strText = Jsoup .connect("http://www.whatismyreferer.com") .referrer("http://www.google.com") .get() .text(); System.out.println(strText); }catch(IOException ioe){ System.out.println("Exception: " + ioe); } } } |
Output
1 |
What is my Referer? What is my Referer? Your HTTP referer: http://www.google.com ... |
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.