在groovyx.net.http.HTTPBuilder上设置不同的超时

时间:2013-07-18 17:58:12

标签: grails groovy

我跟着this post on how to specify a timeout,做了:

def http = new HTTPBuilder(restEndpointUrl);
http.getParams().setParameter("http.socket.timeout", new Integer(2000))

我得到了错误:

Class: groovy.lang.MissingMethodException
Message:No signature of method: groovyx.net.http.HTTPBuilder.getParams() is applicable for argument types: () values: [] Possible solutions: getParser(), getClass(), getHeaders(), getUri()

我可能会把它设置在错误的课堂上,但如果你直接知道我做错了什么,你的评论会非常感激。我是Groovy / Grails的新手。

由于

2 个答案:

答案 0 :(得分:1)

您在基础客户端上设置计时器...

http.getClient().getParams().setParameter("http.socket.timeout", new Integer(2000))

答案 1 :(得分:0)

试试这个,我正在使用带有Grails 2.3.9的http-builder 0.7.1插件:

import groovyx.net.http.HTTPBuilder
import org.apache.http.client.config.RequestConfig
import org.apache.http.config.SocketConfig
import org.apache.http.conn.ConnectTimeoutException
import org.apache.http.impl.client.HttpClients

def timeout = 10000
SocketConfig sc = SocketConfig.custom().setSoTimeout(timeout).build()
RequestConfig rc = RequestConfig.custom().setConnectTimeout(timeout).setSocketTimeout(timeout).build()
def hc = HttpClients.custom().setDefaultSocketConfig(sc).setDefaultRequestConfig(rc).build()        
def http = new HTTPBuilder(restEndpointUrl)
http.client = hc