使用Groovy';s HTTPBuilder,如何设置超时

使用Groovy';s HTTPBuilder,如何设置超时,groovy,httpbuilder,Groovy,Httpbuilder,我试图用Groovy HTTPBuilder设置一个连接超时,但我一直找不到办法 使用普通URL很容易: def client = new URL("https://search.yahoo.com/search?q=foobar") def result = client.getText( readTimeout: 1 ) 这会引发SocketTimeoutException,但这不是我想要的。出于各种原因,我宁愿使用HTTPBuilder或更好的RESTClient 这确实有效:

我试图用Groovy HTTPBuilder设置一个连接超时,但我一直找不到办法

使用普通URL很容易:

def client = new URL("https://search.yahoo.com/search?q=foobar")
def result = client.getText( readTimeout: 1 )
这会引发SocketTimeoutException,但这不是我想要的。出于各种原因,我宁愿使用HTTPBuilder或更好的RESTClient

这确实有效:

    def client = new HTTPBuilder()
    def result = client.request("https://search.yahoo.com/", Method.GET, "*/*") { HttpRequest request ->
        uri.path = "search"
        uri.query = [q: "foobar"]
        request.getParams().setParameter("http.socket.timeout", 1);
    }
但是,request.getParams()已被弃用


就我个人而言,我找不到一种方法将适当的RequestConfig注入到构建器中。

从JavaDoc来看,您似乎是通过使用?该类扩展了HTTPBuilder,它有一个setTimeout(int)方法


看看这个:根据这一点,您似乎可以按照此处的建议获得连接超时:

试试这个,我使用的是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 // millis
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('https://search.yahoo.com/')
http.client = hc

http.get(path:'/search')
我用这个

def timeOut = 10000
HTTPBuilder http = new HTTPBuilder('http://url.com')
http.client.params.setParameter('http.connection.timeout', new Integer(timeOut))
http.client.params.setParameter('http.socket.timeout', new Integer(timeOut))
纯HTTPBuilder:

import org.apache.http.client.config.RequestConfig

def TIMEOUT = 10000
def defaultRequestConfig = RequestConfig.custom()
        .setConnectionRequestTimeout(TIMEOUT)
        .setConnectTimeout(TIMEOUT)
        .setSocketTimeout(TIMEOUT)
        .build()
def client = new HTTPBuilder("uri")
client.setClient(HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build())
RESTClient与一切:

import org.apache.http.auth.AuthScope
import org.apache.http.auth.UsernamePasswordCredentials
import org.apache.http.client.config.RequestConfig
import org.apache.http.conn.ssl.NoopHostnameVerifier
import org.apache.http.conn.ssl.SSLConnectionSocketFactory
import org.apache.http.conn.ssl.TrustSelfSignedStrategy
import org.apache.http.impl.client.BasicCredentialsProvider
import org.apache.http.impl.client.HttpClients
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager
import org.apache.http.ssl.SSLContextBuilder

def restClient = new RESTClient("hostname")

//timeout
def TIMEOUT = 5000
def defaultRequestConfig = RequestConfig.custom()
        .setConnectionRequestTimeout(TIMEOUT)
        .setConnectTimeout(TIMEOUT)
        .setSocketTimeout(TIMEOUT)
        .build()

//basic user/password authentication
def credentials = new UsernamePasswordCredentials("admin", "password")
def credentialsProvider = new BasicCredentialsProvider()
credentialsProvider.setCredentials(AuthScope.ANY, credentials)

//set ssl trust all, no ssl exceptions
def sslContext = new SSLContextBuilder().loadTrustMaterial(null, TrustSelfSignedStrategy.INSTANCE).build()
def sslSocketFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE)

//multithreaded connection manager
def multithreadedConnectionManager = new PoolingHttpClientConnectionManager()

//build client with all this stuff
restClient.setClient(HttpClients.custom()
        .setConnectionManager(multithreadedConnectionManager)
        .setDefaultCredentialsProvider(credentialsProvider)
        .setDefaultRequestConfig(defaultRequestConfig)
        .setSSLSocketFactory(sslSocketFactory)
        .build())

您是否尝试过:
client.client.params.setParameter(“http.socket.timeout”,1000)
最近我自己也需要这样做,但遇到了这样一个问题:设置参数很好。但是,该方法已被弃用。HTTPBuilder似乎还没有现代化。谢谢,好主意。但是,我查看了AsyncHTTPBuilder的代码。它还使用不推荐使用的设置参数的方法。我想没有更好的办法了。好吧,我在答案中添加了一个url。也许这会有帮助。再次感谢,但问题不是让它工作-超时设置很好。问题是AsyncHTTPBuilder正在使用不推荐使用的方法。查看第211行。client.getParams()已被弃用。我有点明白你在这里的意思:是的-你明白了,djangofan。实际上,我很担心HTTPBuilder可能是一个死项目。它仍然使用
getParams()
,OP不想使用它,因为它已被弃用。假设超时时间以毫秒为单位是否安全?文件没有说:(是的,是米利斯。