'Connection timeout with HttpURLConnection in Groovy

I am always getting the "Error Connecting to ${url}" message?

Can anyone please show me my mistake?

        def url = new URL("https://www.google.com")
        HttpURLConnection connection = (HttpURLConnection) url.openConnection()
        connection.setRequestMethod("GET")
        // connection.setConnectTimeout(10000)
        connection.connect()
        if (connection.responseCode == 200 || connection.responseCode == 201) {
            def returnMessage = connection.content

            //print out the full response
            println returnMessage
        } else {
            println "Error Connecting to " + url
        }

| Error 2012-07-05 00:04:05,950 [http-bio-8080-exec-6] ERROR errors.GrailsExceptionResolver - ConnectException occurred when processing request: [GET] /CopperApplications/urlTracker Connection timed out: connect. Stacktrace follows: Message: Connection timed out: connec



Solution 1:[1]

Your code seems correct, and produces the expected(?) results on Groovy 1.7.9 with 1.6.0_33 JVM. There is likely something amiss with your network (as indicated by the connection timeout error).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Lauri Piispanen