2011-05-03 103 views
0

我正在用Java編寫一個網頁抓取工具,但我在代理服務器後面,這使事情變得非常困難。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 

有沒有設置代理服務器設置的更好的辦法?

回答

0

也許系統的代理服務器設置沒有按照你的預期配置。嘗試明確設置JVM系統屬性http.proxyPort,http.proxyHosthttp.nonProxyHosts