External Libraries

Set Jsoup proxy in Java example

Set Jsoup proxy in Java example shows how to set proxy with Jsoup in Java. Example also shows how to set Jsoup proxy for HTTP, HTTPS and specify proxy 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.

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 Jsoup 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.

Above examples sets “127.0.0.1” as proxy host and “3128” as proxy port. Change these values accordingly to set Jsoup proxy.

Setting Jsoup HTTPS proxy

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

Setting Jsoup proxy authentication

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

For HTTP,

For HTTPS,

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

Tags

About the author

rahimv

rahimv

rahimv has over 15 years of experience in designing and developing Java applications. His areas of expertise are J2EE and eCommerce. If you like the website, follow him on Facebook, Twitter or Google Plus.

  • SANJAY GUPTA

    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);
    }

    }
    }

    • rahimv

      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”);

      • SANJAY GUPTA

        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

  • Yoann

    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 ?

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