即使提供的REST端点已启动并且有效,下面的代码也会返回异常。正确处理它,因为DEBUG提示符显示了GET请求的链接。无论提供的REST端点如何,它都在http.request行失败。这是通过点击Run' MyScript'来执行的。在intelliJ 14.1 ..这段代码有什么缺失吗?资料来源:github.com/jgritman/httpbuilder/wiki/GET-Examples
11:42:30.897 [main] DEBUG groovyx.net.http.HTTPBuilder - GET http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=Calvin+and+Hobbes
Caught: java.net.UnknownHostException: ajax.googleapis.com
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