6

i'm trying to use Jsoup to connect to a site but i keep on getting the following error, i have configured everything well inside the following xmls,"setting.xlm" and "pom.xml". can anyone help me to find out what is the main cause of all this. Than you so much.

here is the error that im getting:

Exception in thread "main" java.net.SocketTimeoutException: connect timed out
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:75)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
    at java.net.Socket.connect(Socket.java:579)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:378)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:473)
    at sun.net.www.http.HttpClient.<init>(HttpClient.java:203)
    at sun.net.www.http.HttpClient.New(HttpClient.java:290)
    at sun.net.www.http.HttpClient.New(HttpClient.java:306)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:995)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:931)
    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:849)
    at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:425)
    at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:410)
    at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:164)
    at org.jsoup.helper.HttpConnection.get(HttpConnection.java:153)
    at com.mycompany.mavenproject1.Facebook.main(Facebook.java:23)
------------------------------------------------------------------------
BUILD FAILURE
------------------------------------------------------------------------
Total time: 16.904s
Finished at: Mon May 20 14:00:03 CAT 2013
Final Memory: 19M/47M
------------------------------------------------------------------------
Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:exec (default-cli) on project mavenproject1: Command execution failed. Process exited with an error: 1 (Exit value: 1) -> [Help 1]
Teshte
  • 624
  • 1
  • 7
  • 26
ManEri EJ Mdb
  • 63
  • 1
  • 1
  • 3
  • Are you behind a proxy? – acdcjunior May 21 '13 at 02:12
  • Yes i am behind a proxy, and i was thinking that i need to setup a proxy, because i have installed every dependency required, and that wont be the cause of this error. – ManEri EJ Mdb May 21 '13 at 09:05
  • look at http://stackoverflow.com/questions/6571548/i-get-a-sockettimeoutexception-in-jsoup-read-timed-out –  Jan 15 '14 at 20:19

2 Answers2

8

You are probably unable to reach the internet due to being behind a proxy.

Setting up a proxy for Jsoup is the same as for ordinary URL connections on Java.

Considering your proxy is at 127.0.0.1 and its port is 8182, you can set it as:

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

If you are trying to access through HTTPS, the properties are https.proxyHost and https.proxyPort.

See working example:

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class ExampleJSoupProxy {
    public static void main(String[] args) throws Exception {
        System.setProperty("http.proxyHost", "127.0.0.1");
        System.setProperty("http.proxyPort", "8182");

        Document doc = Jsoup.connect("http://stackoverflow.com").get();
        System.out.println("Obtained Title: " + doc.title());
    }
}

Output:

Obtained Title: Stack Overflow
acdcjunior
  • 132,397
  • 37
  • 331
  • 304
  • 1
    For username/password input: System.setProperty("http.proxyUser", "username"); System.setProperty("http.proxyPassword", "password"); – panagdu Jun 22 '15 at 13:07
1

This does not seem to be a problem in your application. It should be a network problem.

Check if you can open the page you're trying to parse in your browser. If you're using a proxy, try to run your application without proxy and see if it solves the issue.

Ali Hashemi
  • 3,158
  • 3
  • 34
  • 48