0

I am trying to connect Google.com using the below code but getting

java.net.ConnectException: Connection Timed out
. Please help!
private static String getUrlContents(String theUrl) {

    StringBuilder content = new StringBuilder();

    // many of these calls can throw exceptions, so i've just
    // wrapped them all in one try/catch statement.
    try {
        // create a url object
        URL url = new URL(theUrl);

        // create a urlconnection object
        URLConnection urlConnection = url.openConnection();

        // wrap the urlconnection in a bufferedreader
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

        String line;

        // read from the urlconnection via the bufferedreader
        while ((line = bufferedReader.readLine()) != null) {
            content.append(line + "\n");
        }
        bufferedReader.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return content.toString();
}
hc_dev
  • 8,389
  • 1
  • 26
  • 38
ishita
  • 11
  • 1
  • 1
  • 1

2 Answers2

0

I just tried your code and it works fine.

It seems it's a network problem, maybe you don't have Internet access, or firewall is stoping connections. Supposing you are in UNIX system try to:

  1. ping www.google.com -C 1 If this you don't get an answer and you have normal access connection (i.e. from your browser) it may be a firewall problem.

  2. traceroute www.google.com to check the number of hops that should be normally one or two for google's servers.

If you are on CentOS you should try to stop the firewall for testing beacuse it's quite restrictive: sudo systemctl stop firewalld Remember to start it again and add the rules you need to perform your requests.

-3

So, you are not asking how to do: How to read content from an URL in java? That is already answered in Read url to string in few lines of java code.

You described a problem you like to get analysed or even solved:

getting java.net.ConnectException: Connection timed out

See following answer to question Connection timed out. Why?

Things you should look at:

The chances are that your problem is firewall related. My first guess would be that you don't have the correct environment variables or Java system properties set to tell the JVM to use a local proxy server for outgoing HTTP / HTTPS requests

An alternative approach to read content from an URL may be the free open-source JSoup library. Take a look on how to use it for your case: http://jsoup.org/cookbook/input/load-document-from-url

With JSoup you can even parse the returned HTML document elegantly.

hc_dev
  • 8,389
  • 1
  • 26
  • 38
  • 1
    He doesn't need to know how to use a timeout. He is already getting a timeout. He needs to know how *not* to get one. And you can't extend the default connect timeout. You can only reduce it. And neither of your citations says anything different. – user207421 Feb 01 '19 at 11:09
  • 1
    @user207421 adapted my answer to solve the specified `ConnectException` and suggested to rename this question's title. I researched on stackoverflow for this exception and found a lot java-networking topics where you always criticized answers but did not provide some yourself. Your insight may also be valueable if you provide (better) answers yourself. – hc_dev Feb 01 '19 at 17:04