0

I am running a simple java program to connect to the hard coded URL "http://www.google.co.in/?gws_rd=ssl" and when trying to fetch a response code: it throws the connection timed out exception.

The code snippet as follows:

url = new URL("http://www.google.co.in/?gws_rd=ssl");
urlConn = (HttpURLConnection) url.openConnection();
**int responseCode = urlConn.getResponseCode(); ---> exception line**

Please help if any one have the resolution?

Prakruti Pathik
  • 392
  • 4
  • 17

3 Answers3

1

Did you check with urlConn = (HttpsURLConnection) url.openConnection();.

Pierre.Vriens
  • 2,117
  • 75
  • 29
  • 42
Yasothar
  • 435
  • 1
  • 7
  • 24
0

Try this one,

public static String callURL(String myURL) {
    System.out.println("Requeted URL:" + myURL);
    StringBuilder sb = new StringBuilder();
    URLConnection urlConn = null;
    InputStreamReader in = null;
    try {
        URL url = new URL(myURL);
        urlConn = url.openConnection();
        if (urlConn != null)
            urlConn.setReadTimeout(60 * 1000);
        if (urlConn != null && urlConn.getInputStream() != null) {
            in = new InputStreamReader(urlConn.getInputStream(),
                    Charset.defaultCharset());
            BufferedReader bufferedReader = new BufferedReader(in);
            if (bufferedReader != null) {
                int cp;
                while ((cp = bufferedReader.read()) != -1) {
                    sb.append((char) cp);
                }
                bufferedReader.close();
            }
        }
    in.close();
    } catch (Exception e) {
        throw new RuntimeException("Exception while calling URL:"+ myURL, e);
    } 

    return sb.toString();
}
Abdul Manaf
  • 4,933
  • 8
  • 51
  • 95
0

The proable reason that you are behind a proxy. Confirm that you are able to connect to the URL using the browser. Set the proxy settings for JVM. This is the link to set the proxy settings.

Community
  • 1
  • 1
pawinder gupta
  • 1,225
  • 16
  • 35