Java代理问题 - 连接超时

时间:2011-05-03 18:00:40

标签: java proxy

我正在用Java编写一个web scraper,但我在代理服务器后面,这使得事情变得非常困难。

这是连接代码:

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

有更好的方法来设置代理设置吗?

2 个答案:

答案 0 :(得分:0)

您可以使用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

答案 1 :(得分:0)

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