【问题标题】:Using Groovy's HTTPBuilder, how do you set timeouts使用 Groovy 的 HTTPBuilder,如何设置超时
【发布时间】:2016-06-08 17:55:13
【问题描述】:

我正在尝试使用 Groovy HTTPBuilder 设置连接超时,但我终其一生都找不到方法。

使用普通的 ol' 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 注入构建器的方法。

【问题讨论】:

  • 你试过了吗:client.client.params.setParameter("http.socket.timeout", 1000)
  • 我自己最近需要这个并且遇到了这个:gist.github.com/axeda/5189102
  • 设置参数效果很好。但是,该方法已被弃用。 HTTPBuilder 似乎还没有现代化。

标签: groovy httpbuilder


【解决方案1】:

试试这个,我用的是 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')

【讨论】:

  • 假设超时以毫秒为单位是否安全?文档没有说。 :(
  • 是的,它的毫秒。
【解决方案2】:

纯 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())

【讨论】:

    【解决方案3】:

    我是这样用的

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

    【讨论】:

    • 这仍然使用getParams(),OP 不想使用它,因为它已被弃用。
    【解决方案4】:

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

    看这个:http://www.kellyrob99.com/blog/2013/02/10/groovy-and-http/ 据此,您似乎可以按照此处的建议来获得连接超时:Java HTTP Client Request with defined timeout

    【讨论】:

    • 谢谢,好主意。但是,我查看了 AsyncHTTPBuilder 的代码。它还使用不推荐使用的设置参数的方法。我想没有更好的办法了。
    • 好的,我在答案中添加了一个网址。也许这会有所帮助。
    • 再次感谢,但问题不在于让它工作 - 超时设置很好。问题是 AsyncHTTPBuilder 正在使用已弃用的方法。查看第 211 行 code 。 client.getParams() 已弃用。
    • 是的 - 你明白了,djangofan。实际上,我开始担心 HTTPBuilder 可能是一个死项目。
    猜你喜欢
    • 2015-11-10
    • 1970-01-01
    • 2015-10-11
    • 1970-01-01
    • 1970-01-01
    • 2011-06-17
    • 1970-01-01
    • 1970-01-01
    • 2016-01-11
    相关资源
    最近更新 更多