1
try { 
{
    long startTime = System.currentTimeMillis();

        String source="s";
        String source1="s";
        URL google = new URL("http://google.com/"); 
        HttpURLConnection yc =(HttpURLConnection)google.openConnection(); 
        BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream())); 
        String inputLine;


        while ((inputLine = in.readLine()) != null) { 
            source=source.concat(inputLine);
        }
        in.close();

        yc.disconnect();

    }

long endTime1 = System.currentTimeMillis();
System.out.println("Total elapsed time in execution of method callMethod() is :"+ (endTime1-startTime));


    } 
}

when i tried the above through command prompt i got

java.net.UnknownHostException: google.com 
at java.net.PlainSocketImpl.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 ScagntJavaHttp.httpMakeRequest(ScagntJavaHttp.java:185) 
at test.main(test.java:23)

Can any help me in resolving this one?

jjnguy
  • 136,852
  • 53
  • 295
  • 323
crazypaladin
  • 453
  • 4
  • 7
  • 17

2 Answers2

3

Try removing http:// from your host url when you get java.net.UnknownHostException and check your internet connection and the host exists (probably safe with google . . .)

ono2012
  • 4,967
  • 2
  • 33
  • 42
  • 3
    If you remove http://, java.net.MalformedURLException will be thrown. -1 for this answer, didn't helped.. – nick Jul 27 '16 at 16:01
3

I believe it's a proxy problem. Try to see if you have a proxy definition in your browser and then set it:

    ProxySelector.setDefault(new ProxySelector() {

        @Override
        public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
            throw new RuntimeException("Proxy connect failed", ioe);
        }

        @Override
        public List select(URI uri) {
            return Arrays
                .asList(new Proxy(Proxy.Type.HTTP,
                                  new InetSocketAddress(proxyHost,
                                                        proxyPort)));
        }
    });

To see if you have proxy definition in IE, go to Tools - Internet Options -- Connections -- Lan Settings

Tarlog
  • 10,024
  • 2
  • 43
  • 67