我對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
我真的認爲你應該發佈你的實際代碼。你發佈的代碼工作正常。 – BackSlash
那麼這是什麼問題!? :/ 它是Eclipse只是Actin瘋狂!? –
此代碼在我的結尾工作正常... – Mac