2012-10-18 129 views
2

我使用的代碼從Sun's java tutorialURLConnection.getInputStream():連接超時

import java.net.*; 
import java.io.*; 

public class URLConnectionReader { 
    public static void main(String[] args) throws Exception { 
     URL yahoo = new URL("http://www.yahoo.com/"); 
     URLConnection yc = yahoo.openConnection(); 
     BufferedReader in = new BufferedReader(
           new InputStreamReader(
           yc.getInputStream())); 
     String inputLine; 

     while ((inputLine = in.readLine()) != null) 
      System.out.println(inputLine); 
     in.close(); 
    } 
} 

堆棧跟蹤相同Connection timed out. Why?

我懷疑這可能是問題,但防火牆

  1. ping to google.com是好的
  2. 它在瀏覽器中工作
  3. 這種方法失敗對每個URL我提供
  4. 我在其他程序中使用DJ WebBrowser組件和它的作品沒關係,瀏覽器

我怎麼能調查更多關於這個問題? 當我運行代碼時,我能知道哪些端口號將被使用嗎?

感謝

+0

它是一個公司的網絡。是的,很有可能。我沒有在其他地方測試過它。 –

+0

如果您使用Firefox,請轉至工具>選項>高級>網絡>設置,然後查看是否配置了任何代理網址。 –

+0

什麼防火牆設置?你在什麼操作系統上? –

回答

4

查找所使用的公司,並將其設置在程序的代理。從引用的代碼[1]

//Set the http proxy to webcache.mydomain.com:8080 

System.setProperty("http.proxyHost", "webcache.mydomain.com"); 
System.setPropery("http.proxyPort", "8080"); 

// Next connection will be through proxy. 
URL url = new URL("http://java.sun.com/"); 
InputStream in = url.openStream(); 

// Now, let's 'unset' the proxy. 
System.setProperty("http.proxyHost", null); 

// From now on http connections will be done directly. 

[1] - http://docs.oracle.com/javase/6/docs/technotes/guides/net/proxies.html

+0

上運行它是正確的!謝謝。最後的unset屬性應該有「」,而不是null。 –