【问题标题】:Java proxy issues - Connection Timed OutJava 代理问题 - 连接超时
【发布时间】:2011-08-17 22:29:56
【问题描述】:

我正在用 Java 编写网络爬虫,但我在代理服务器后面,这让事情变得非常困难。

这是连接代码:

public void scrape(String url, String filename) throws Exception {
    this.url = url;
    this.filename = filename;

    System.out.println("Scraping " + url);
    System.out.println("Saving to \"" + this.filename + "\"");

    try {
        makeConnection();
        createStream();
        writeToFile();
        System.out.println("Scrape was successful");
    } catch (Exception e) {
        System.err.println("Error: " + e.getMessage());
    }
}

private void makeConnection() throws Exception {
    // Set proxy info
    System.setProperty("java.net.useSystemProxies", "true");

    URL address = new URL(url);
    connection  = address.openConnection();
}

这是输出:

Scraping http://feeds.bbci.co.uk/news/northern_ireland/rss.xml
Saving to "../rss/northern_ireland.xml"
Error: Connection timed out

有没有更好的代理设置方法?

【问题讨论】:

    标签: java proxy


    【解决方案1】:

    可以使用 Java 1.5 中引入的 java.net.Proxy 类... http://download.oracle.com/javase/1.5.0/docs/api/java/net/Proxy.html

    可以在此处找到有关如何使用它的简要说明:http://download.oracle.com/javase/6/docs/technotes/guides/net/proxies.html

    【讨论】:

      【解决方案2】:

      可能系统的代理设置未按您的预期配置。尝试显式设置 JVM 系统属性 http.proxyPorthttp.proxyHosthttp.nonProxyHosts

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-10-21
        • 2018-10-12
        • 2011-06-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-29
        • 2017-03-13
        相关资源
        最近更新 更多