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.

1

2

3

4

5

6

7

8

9

10

11

12

java.net.SocketTimeoutException: connect timed out

at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)

at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)

at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)

at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)

at java.net.AbstractPlainSocketImpl.connect(Unknown Source)

at java.net.PlainSocketImpl.connect(Unknown Source)

at java.net.SocksSocketImpl.connect(Unknown Source)

at java.net.Socket.connect(Unknown Source)

at sun.net.NetworkClient.doConnect(Unknown Source)

at sun.net.www.http.HttpClient.openServer(Unknown Source)

…

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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

packagecom.javacodeexamples.libraries.jsoup;

importjava.io.IOException;

importorg.jsoup.Jsoup;

publicclassJsoupSetProxyExample{

publicstaticvoidmain(String[]args){

try{

//set HTTP proxy host to 127.0.0.1 (localhost)

System.setProperty("http.proxyHost","127.0.0.1");

//set HTTP proxy port to 3128

System.setProperty("http.proxyPort","3128");

StringstrText=

Jsoup

.connect("http://www.example.com")

.userAgent("Mozilla/5.0")

.timeout(10*1000)

.get()

.text();

System.out.println(strText);

}catch(IOException ioe){

System.out.println("Exception: "+ioe);

}

}

}

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.

1

2

System.setProperty("https.proxyHost","127.0.0.1");

System.setProperty("https.proxyPort","3128");

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.

About the author

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.