1

I want the request of http get request using the following java code. But, uncertainly i am getting the following exception.

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

The Error or exception is as follows:

Exception in thread "main" java.net.ConnectException: Connection timed out: connect at java.net.DualStackPlainSocketImpl.connect0(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:337) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:198) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:180) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391) at java.net.Socket.connect(Socket.java:579) at java.net.Socket.connect(Socket.java:528) at sun.net.NetworkClient.doConnect(NetworkClient.java:180) at sun.net.www.http.HttpClient.openServer(HttpClient.java:388) at sun.net.www.http.HttpClient.openServer(HttpClient.java:483) at sun.net.www.http.HttpClient.<init>(HttpClient.java:213) at sun.net.www.http.HttpClient.New(HttpClient.java:300) at sun.net.www.http.HttpClient.New(HttpClient.java:316) at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:992) at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:928) at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:846) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1296) at API.main(API.java:8) 
2
  • Are you sure throwing exceptions? I have tried above function.It working fine for me. Commented Sep 23, 2013 at 10:10
  • Your code works fine for me Commented Sep 23, 2013 at 10:12

2 Answers 2

4

I'm guessing you have a firewall and it's blocking this HTTP get request.

You code should work as you expect it. Something outside the Java runtime is preventing the connection.

Sign up to request clarification or add additional context in comments.

Comments

1

Check whether you can ping to http://www.oracle.com/.

If you can ping check whether you are behind a proxy server.

If so provide -Dhttp.proxyHost and -Dhttp.proxyPort while starting the JVM

java -Dhttp.proxyHost=<Your Proxy Server Name/IP> -Dhttp.proxyPort=<Your Proxy Server Port> 

2 Comments

Yes, i can ping oracle.com/...how to provide -Dhttp.proxyHost and -Dhttp.proxyPort while starting the JVM???
JVM's allow you to define system properties from the command line. You can perform this by using the -D argument, in the form -Dproperty=value

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.