我正在尝试使用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注入构建器的方法。
答案 0 :(得分:6)
试试这个,我正在使用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/search?q=foobar')
http.client = hc
答案 1 :(得分:0)
从JavaDoc开始,您可以使用AsyncHttpBuilder代替它吗?该类扩展了HTTPBuilder,它有一个 setTimeout(int)方法。
请注意:http://www.kellyrob99.com/blog/2013/02/10/groovy-and-http/根据这一点,您似乎可以按照此处的建议来暂停连接:Java HTTP Client Request with defined timeout
答案 2 :(得分:0)
纯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 :(得分:-1)
我像这样使用
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))