0

I'm pretty new to java , actually I have just started learning it I tried to do an exercise and the exercise was to read the first five lines of a webpage for start I wrote this code :

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() ;
    }

}

but when I run this piece of code through Eclipse , I get this :

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)

Why is this happening !?

and ofcourse when I dont put the throws Exception part at the begining I get the malformed url exception !

PS : my internet connection works just fine !

Can Somebody please help me and explain why is this happening while doing that ? I have a pretty good c++ background so feel free to explain as deep as you can :D

6
  • I really think you should post your actual code. The code you posted works fine. Commented Jul 27, 2013 at 20:07
  • so what is the problem then !? :/ Is it Eclipse Just Actin Crazy !? Commented Jul 27, 2013 at 20:07
  • This code is working fine at my end... Commented Jul 27, 2013 at 20:07
  • @AshkanKzme The problem is that you are not showing us the real code that causes the exception to be thrown. Commented Jul 27, 2013 at 20:08
  • this is my Eclipse window : upload7.ir/images/62162506249029350911.jpg Commented Jul 27, 2013 at 20:11

1 Answer 1

2

It seems like your program can't connect to the URL. Are u using internet behind proxy? If so, then make sure ur program is configured accordingly. One way is to use this code:

System.setProperty("http.proxyHost", "proxy.mydomain.com");
System.setPropery("http.proxyPort", "8080");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.