5

我正在尝试使用 Jsoup 连接到一个站点,但我不断收到以下错误,我已经在以下 xmls、“setting.xlm”和“pom.xml”中配置了所有内容。谁能帮我找出这一切的主要原因是什么。比你好多了。

这是我得到的错误:

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]
4

2 回答 2

8

由于位于代理后面,您可能无法访问互联网。

为 Jsoup 设置代理与Java 上的普通连接相同URL

考虑到您的代理位于127.0.0.1且其端口为8182,您可以将其设置为:

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

如果您尝试通过 HTTPS 访问,则属性为https.proxyHosthttps.proxyPort

请参阅工作示例:

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

输出:

Obtained Title: Stack Overflow
于 2013-05-21T15:05:20.070 回答
1

这在您的应用程序中似乎不是问题。应该是网络问题。

检查您是否可以在浏览器中打开您尝试解析的页面。如果您使用的是代理,请尝试在没有代理的情况下运行您的应用程序,看看它是否能解决问题。

于 2013-05-21T09:08:30.657 回答