Skip to content

Set Jsoup proxy in Java example

Set Jsoup proxy in Java example shows how to set proxy with Jsoup in Java. The example also shows how to set HTTP and HTTPS proxy with username and password.

How to set Jsoup proxy?

If you are behind the internet proxy, you will get “java.net.SocketTimeoutException: connect timed out” exception while trying to connect to any URL using Jsoup like given below. (time out full example)

You need to set the proxy before you can access any URLs using Jsoup. Under the hood, Jsoup uses Java’s built-in Connection and HTTPURLConnection classes. That means we can set the proxy in the same standard Java way using system properties.

Setting HTTP proxy

Use setProperty method of System class to set proxy host and proxy port properties before you connect to a URL using Jsoup as given below.

The above example sets “127.0.0.1” as a proxy host and “3128” as a proxy port. Change these values according to your proxy server details.

Setting HTTPS proxy

If you are trying to parse HTTPS pages using the Jsoup, you also need to set the HTTPS proxy. It can be done by setting below given properties.

Setting proxy authentication

Does your proxy server need authentication (username and password) to access the internet? If yes, you also need to set a proxy username and password using the below-given properties.

For HTTP,

For HTTPS,

UPDATE (Recommended way): 

If you are using the Jsoup 1.9.1 version or later, you should use the proxy method of the Connection class.

It sets the connection proxy to provided host and port values.

The Jsoup connection class also now provides an overloaded proxy method that accepts the Proxy object.

If you pass null as a proxy object, it will remove the previously set proxy.

Additionally, it is always a best practice to set user agent and referrer headers in the Jsoup connection.

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

4 comments

  1. code is as follows:

    import java.io.IOException;

    import org.jsoup.Jsoup;

    public class JsoupSetProxyExample {

    public static void main(String[] args) {

    try{

    //set HTTP proxy host to 127.0.0.1 (localhost)
    System.setProperty(“http.proxyHost”, “10.1.1.18”);

    //set HTTP proxy port to 3128
    System.setProperty(“http.proxyPort”, “80”);
    System.setProperty(“http.proxyUser”, “user_id”);
    System.setProperty(“http.proxyPassword”, “password”);

    String strText =
    Jsoup
    .connect(“http://htmlunit.sourceforge.net/gettingStarted.html”)
    .userAgent(“Mozilla/5.0”)
    .timeout(10 * 1000)
    .get()
    .text();

    System.out.println(strText);

    }catch(IOException ioe){
    System.out.println(“Exception: ” + ioe);
    }

    }
    }

    1. You need to provide proxy user id and password in order for this to work.

      System.setProperty(“http.proxyUser”, “your_user_id”);
      System.setProperty(“http.proxyPassword”, “your_password”);

      1. yes i provided the my proxy user_id and my_password , i have not posted in this web page.
        in following order

        System.setProperty(“http.proxyHost”, “10.1.1.18”);
        System.setProperty(“http.proxyPort”, “80”);
        System.setProperty(“http.proxyUser”, “my_user_id”);
        System.setProperty(“http.proxyPassword”, “my_password”);

        but the same exception is coming as below:

        Exception: org.jsoup.HttpStatusException: HTTP error fetching URL. Status=407, URL=http://htmlunit.sourceforge.net/gettingStarted.html

  2. Hi !
    So you set the proxy for the whole connections from the JVM.
    Do you have an idea to set the proxy for this Jsoup.connect() only ?

Leave a Reply

Your email address will not be published.