2

I use the code from Sun's java tutorial

import java.net.*; import java.io.*; public class URLConnectionReader { public static void main(String[] args) throws Exception { 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(); } } 

Stack trace is same as Connection timed out. Why?

I suspect that might be problem with firewall but

  1. ping to google.com is okay
  2. it works in browser
  3. this approach fails for every URL I provide
  4. I use DJ WebBrowser component in other program and it works okay as a browser

How can I investigate more about this problem? Can I get to know which port numbers are going to be used when I run the code?

Thanks

5
  • It is a company network. Yes, with high probability. I have not tested it anywhere else. Commented Oct 18, 2012 at 14:33
  • If you are using Firefox, go to Tools > Options > Advanced > Network > Settings and see if there is any proxy url being configured. Commented Oct 18, 2012 at 14:36
  • What about any firewall settings? What OS are you on? Commented Oct 18, 2012 at 14:37
  • In Google Chrome it says there is Automatic Configuration Script configured and not Proxy Server directly. I am using Windows 7, there is no personal firewall, but I am sure there is company's firewall. Commented Oct 18, 2012 at 14:40
  • Are you running this code on your companies server or its just on your local machine? If its on server I think that its a firewall issue. Commented Oct 18, 2012 at 15:09

2 Answers 2

4

Find the proxy that is used by your company and set it in your program. Quoting code from [1]

//Set the http proxy to webcache.mydomain.com:8080 System.setProperty("http.proxyHost", "webcache.mydomain.com"); System.setPropery("http.proxyPort", "8080"); // Next connection will be through proxy. URL url = new URL("http://java.sun.com/"); InputStream in = url.openStream(); // Now, let's 'unset' the proxy. System.setProperty("http.proxyHost", null); // From now on http connections will be done directly. 

[1] - http://docs.oracle.com/javase/6/docs/technotes/guides/net/proxies.html

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

1 Comment

that's correct! thanks. The last unset property should have "", not null.
0

There is also a HttpURLConnection that might work better in some cases. It doesn't appear to disconnect in the same way.

URL url = new URL(address); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setConnectTimeout(callTimeout); 

and so on.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.