Connection timeout with HttpURLConnection in Groov

2019-08-10 20:50发布

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

标签: url groovy
1条回答
Animai°情兽
2楼-- · 2019-08-10 21:32

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).

查看更多
登录 后发表回答