Link to home
Start Free TrialLog in
Avatar of abhinav90
abhinav90

asked on

java.net unable to connect to URL

I am trying to connect to a url using java.net to connect to a url but it keeps giving me an UnknownHostException: www.yahoo.com. I have tried everything. The firewall is not blocking java, i am able to ping the website from the computer running the code. I also tested with adding a line for proxy's to allow for the connection but that did not work either. I tried some other websites hosted on my servers as well but with no luck.
import java.net.*;
import java.io.*;

public class URLconnectionReader {
    public static void main(String[] args) throws Exception {
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("77.95.208.230", 8080));
        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();
    }
}

Open in new window

Avatar of abhinav90
abhinav90

ASKER

i just tested something. If i change the host to localhost and try and access some http content hosted on my wamp server, then the output was perfect. This is ofcourse after removing the proxy statement on line 6. So i guess the code syntax is fine, there is some other issue.
ASKER CERTIFIED SOLUTION
Avatar of Venabili
Venabili
Flag of Bulgaria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial