Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception in thread "main" java.net.SocketTimeoutException: connect timed out at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)

Tags:

jsoup

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]
like image 448
ManEri EJ Mdb Avatar asked May 20 '13 12:05

ManEri EJ Mdb


People also ask

How do I resolve Java net SocketTimeoutException connect timed out?

A possible solution for this problem within the Tomcat web application is to modify the CONTEXT. XML file, and modify the CONNECTOR definition that governs the workstation browser connectivity to the Tomcat server. Specifically, modify the connectionTimeout value. Increase this value to suppress the error condition.

Why does Java net SocketTimeoutException connect timed out?

Your Java socket is timing out means that it takes too long to get respond from other device and your request expires before getting response. This exception is occurring on following condition. Server is slow and default timeout is less, so just put timeout value according to you.

How do I fix socket timeout exception?

Using try/catch/finally If you are a developer, so you can surround the socket connection part of your code in a try/catch/finally and handle the error in the catch. You might try connecting a second time, or try connecting to another possible socket, or simply exit the program cleanly.

How do I fix connection timeout in Java?

As a result, a “connection timed out” error can occur when a client is trying to establish a connection to a server. Therefore, we should check the firewall settings to see if it's blocking a port before binding it to a service.


2 Answers

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
like image 92
acdcjunior Avatar answered Oct 08 '22 17:10

acdcjunior


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.

like image 44
Ali Hashemi Avatar answered Oct 08 '22 17:10

Ali Hashemi