REST客户端的groovy未知主机异常

REST客户端的groovy未知主机异常,rest,intellij-idea,groovy,httpbuilder,Rest,Intellij Idea,Groovy,Httpbuilder,下面的代码返回异常,即使提供的REST端点已启动且有效。当调试提示显示GET请求的链接时,它被正确处理。无论提供的REST端点是什么,它都会在http.request行失败。这是通过在intelliJ 14.1上点击Run“MyScript”来执行的。。这段代码缺少什么吗?来源:github.com/jgritman/httpbuilder/wiki/GET-Examples 11:42:30.897[main]调试groovyx.net.http.HTTPBuilder-获取 MyScript

下面的代码返回异常,即使提供的REST端点已启动且有效。当调试提示显示GET请求的链接时,它被正确处理。无论提供的REST端点是什么,它都会在http.request行失败。这是通过在intelliJ 14.1上点击Run“MyScript”来执行的。。这段代码缺少什么吗?来源:github.com/jgritman/httpbuilder/wiki/GET-Examples

11:42:30.897[main]调试groovyx.net.http.HTTPBuilder-获取

MyScript.groovy

import groovyx.net.http.ContentType
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.RESTClient
import groovyx.net.http.Method

def http = new HTTPBuilder()

http.request( 'http://ajax.googleapis.com', Method.GET, ContentType.JSON ) { req ->
uri.path = '/ajax/services/search/web'
uri.query = [ v:'1.0', q: 'Calvin and Hobbes' ]
headers.'User-Agent' = "Mozilla/5.0 Firefox/3.0.4"
headers.Accept = 'application/json'

response.success = { resp, reader ->
    assert resp.statusLine.statusCode == 200
    println "Got response: ${resp.statusLine}"
    println "Content-Type: ${resp.headers.'Content-Type'}"
    println reader.text
}

    response.'404' = {
        println 'Not found'
    }
}
运行MyScript.groovy异常

11:52:32.133 [main] DEBUG groovyx.net.http.HTTPBuilder - GET http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=Calvin+and+Hobbes
11:52:32.395 [main] DEBUG o.a.h.i.c.BasicClientConnectionManager - Get connection for route {}->http://ajax.googleapis.com
11:52:32.413 [main] DEBUG o.a.h.i.conn.DefaultClientConnection - Connection org.apache.http.impl.conn.DefaultClientConnection@2b175c00 closed
11:52:32.413 [main] DEBUG o.a.h.i.conn.DefaultClientConnection - Connection org.apache.http.impl.conn.DefaultClientConnection@2b175c00 shut down
11:52:32.414 [main] DEBUG o.a.h.i.c.BasicClientConnectionManager - Releasing connection org.apache.http.impl.conn.ManagedClientConnectionImpl@3eb81efb
11:52:32.414 [main] DEBUG o.a.h.i.c.BasicClientConnectionManager - Connection can be kept alive for 9223372036854775807 MILLISECONDS
Caught: java.net.UnknownHostException: ajax.googleapis.com
java.net.UnknownHostException: ajax.googleapis.com

脚本还可以,这不是groovy的问题。您的DNS可能无法解析ajax.googleapis.com,或者站点已关闭(它现在对我有效…请重试)。感谢您的帮助。这确实是一个DNS问题。这段代码工作得很好
11:52:32.133 [main] DEBUG groovyx.net.http.HTTPBuilder - GET http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=Calvin+and+Hobbes
11:52:32.395 [main] DEBUG o.a.h.i.c.BasicClientConnectionManager - Get connection for route {}->http://ajax.googleapis.com
11:52:32.413 [main] DEBUG o.a.h.i.conn.DefaultClientConnection - Connection org.apache.http.impl.conn.DefaultClientConnection@2b175c00 closed
11:52:32.413 [main] DEBUG o.a.h.i.conn.DefaultClientConnection - Connection org.apache.http.impl.conn.DefaultClientConnection@2b175c00 shut down
11:52:32.414 [main] DEBUG o.a.h.i.c.BasicClientConnectionManager - Releasing connection org.apache.http.impl.conn.ManagedClientConnectionImpl@3eb81efb
11:52:32.414 [main] DEBUG o.a.h.i.c.BasicClientConnectionManager - Connection can be kept alive for 9223372036854775807 MILLISECONDS
Caught: java.net.UnknownHostException: ajax.googleapis.com
java.net.UnknownHostException: ajax.googleapis.com