External Libraries Java Exceptions

Jsoup SocketTimeoutException Read timed out Connect timed out fix

Jsoup SocketTimeoutException read timed out, connect timed out example shows how to fix SocketTimeoutException while using Jsoup in Java. Example also shows how to set timeout in Jsoup.

How to fix Jsoup java.net.SocketTimeoutException: Read timed out exception?

You may encounter “java.net.SocketTimeoutException: Read timed out” exception while using Jsoup. Here is the program which gave me the exception.



The problem is the default Jsoup timeout which is 3 seconds. If you encounter the exception “java.net.SocketTimeoutException: Read timed out”, it means that time our program took to read the requested webpage was exceeded the default timeout time (3 seconds).

You need to increase the timeout Jsoup uses to fix the problem using timeout method of Connection class.

This method sets connect and read timeout both. Please note that the timeout is in milliseconds.

How to fix Jsoup java.net.SocketTimeoutException: Connect timed out exception?

Another exception Jsoup may throw is “java.net.SocketTimeoutException: Connect timed out”. This exception means the time taken by our program to connect to the requested URL exceeded the timeout Jsoup uses.

Here is how to fix/resolve the “Connect time out” exception.

1) Make sure you are connected to the internet. Try to open same URL in the browser and see if it opens the page.
2) Specify Jsoup connection time out before getting the document as given below.

3) Are you behind the proxy? If yes, set Jsoup proxy.
4) It is always best practice to set user agent and referrer while making a Jsoup connection.

Refer to Jsoup examples for more tips and tricks. Please let us know your views in the comments section below.