External Libraries Java Exceptions

Jsoup error 403 Forbidden exception fix

Jsoup error 403 Forbidden exception fix example shows how to fix error 403 – Forbidden exception while using Jsoup in Java. Jsoup throws “org.jsoup.HttpStatusException: HTTP error fetching URL. Status=403” exception.

How to solve error 403 – forbidden exception while using Jsoup?

When trying to parse a website, I encountered “org.jsoup.HttpStatusException: HTTP error fetching URL. Status=403”. Here is the code which was used.

Output

HTTP status (response code) returned is 403.

What is HTTP 403 status code?

From Wikipedia,

A web server may return a 403 Forbidden HTTP status code in response to a request from a client for a web page or resource to indicate that the server can be reached and understood the request, but refuses to take any further action.

How to resolve error 403 while using Jsoup?

May of the websites filter web requests against the list of known user agents and denies access to unknown or bot user agents. Jsoup uses the Java version as a user agent string while making the request which could fall into the later category of bots and that could be the reason to get the 403 error – access is forbidden.

Simple solution to this problem is to specify user agent string while connecting to the website using Jsoup as given below.

Still getting Jsoup 403 error? Other possible causes of getting 403 error – access is forbidden is given below.

1) Specify HTTP referrer while making request using Jsoup.
2) If you are crawling the web too fast, website’s firewall may block you IP. Wait for few milliseconds before making next request using Thread.sleep method.

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

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