I'm trying to set a connection timeout with Groovy HTTPBuilder and for the life of me can't find a way.
Using plain ol' URL it's easy:
def client = new URL("https://search.yahoo.com/search?q=foobar")
def result = client.getText( readTimeout: 1 )
This throws a SocketTimeoutException, but that's not quite what I want. For a variety of reasons, I'd rather use HTTPBuilder or better RESTClient.
This does work:
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);
}
However request.getParams() has been deprecated.
For the life of me I can't find a way to inject a proper RequestConfig into the builder.
Try this, I'm using 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')
pure 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 with everything:
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())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With