Skip to content

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. The example also shows how to set the 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 the Jsoup. Here is the program which gave me the exception.

Output

Fix: 

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 the 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 the 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 creating a Jsoup connection.

Many times setting property user agent and referrer values solves this problem.

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.