2013-07-27 70 views
0

我對Java很新,其實我剛開始學習它 我試圖做一個練習,練習是讀取網頁的前五行 開始我寫驗證碼:獲取未處理的異常類型格式不正確的url異常

import java.io.* ; 

import java.net.URL ; 

class testcode { 

    public static void main(String[] args) throws Exception { 
     URL address = new URL("http://www.yahoo.com/") ; 
     InputStream is = address.openStream() ; 
     InputStreamReader isr = new InputStreamReader(is) ; 
     BufferedReader reader = new BufferedReader(isr) ; 
     String line = reader.readLine() ; 
    } 

} 

但是當我通過Eclipse中運行這段代碼,我得到這個:

Exception in thread "main" java.net.ConnectException: Connection refused: connect 
at java.net.DualStackPlainSocketImpl.connect0(Native Method) 
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) 
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) 
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) 
at java.net.AbstractPlainSocketImpl.connect(Unknown Source) 
at java.net.PlainSocketImpl.connect(Unknown Source) 
at java.net.SocksSocketImpl.connect(Unknown Source) 
at java.net.Socket.connect(Unknown Source) 
at java.net.Socket.connect(Unknown Source) 
at sun.net.NetworkClient.doConnect(Unknown Source) 
at sun.net.www.http.HttpClient.openServer(Unknown Source) 
at sun.net.www.http.HttpClient.openServer(Unknown Source) 
at sun.net.www.http.HttpClient.<init>(Unknown Source) 
at sun.net.www.http.HttpClient.New(Unknown Source) 
at sun.net.www.http.HttpClient.New(Unknown Source) 
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source) 
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source) 
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source) 
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) 
at java.net.URL.openStream(Unknown Source) 
at test.testcode.main(testcode.java:10) 

這究竟是爲什麼!?

andcourse當我不投擲異常部分在開始我得到畸形的url異常!

PS:我的互聯網連接工作得很好!

有人請幫助我,解釋爲什麼會發生這種情況嗎? 我有一個相當不錯的C++背景,所以請儘可能深入地解釋:D

+0

我真的認爲你應該發佈你的實際代碼。你發佈的代碼工作正常。 – BackSlash

+0

那麼這是什麼問題!? :/ 它是Eclipse只是Actin瘋狂!? –

+0

此代碼在我的結尾工作正常... – Mac

回答

2

看起來你的程序無法連接到URL。你是否在代理後面使用互聯網?如果是這樣,那麼確保你的程序已經配置好了。一種方法是使用此代碼:

System.setProperty("http.proxyHost", "proxy.mydomain.com"); 
System.setPropery("http.proxyPort", "8080"); 
+0

謝謝,是的,這是問題,我會加一個你,但我不能! –

+0

好吧,那很傷心... 反正,很高興它解決了我們的問題! – Jazib

相關問題