Amrmsmb
Amrmsmb

Reputation: 1

Error when connecting to a website

I tried to run the following code :

public class URLConnectionReader {
    public static void main(String[] args) throws Exception {
        URL oracle = new URL("http://www.google.com/");
        URLConnection yc = oracle.openConnection();
        yc.connect();
        BufferedReader in = new BufferedReader(new InputStreamReader(
                                yc.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) 
            System.out.println(inputLine);
        in.close();
    }
}

But the console displays nothing and when i tried to add yc.connect the console displayed the following errors:

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 internet01.URLConnectionReader.main(URLConnectionReader.java:18)

Update: i connect to the internet through proxy.

Upvotes: 0

Views: 226

Answers (1)

nullpotent
nullpotent

Reputation: 9260

You're connecting through proxy, JVM probably isn't aware of that.

Run you program with these arguments

-Dhttp.proxyHost=proxy.ip -Dhttp.proxyPort=proxy.port

Or set it within your program...

See the answer by Chris Carruthers, gr5

Upvotes: 1

Related Questions