Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Groovy's HTTPBuilder, how do you set timeouts

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.

like image 340
Steve s. Avatar asked Feb 25 '16 19:02

Steve s.


Video Answer


2 Answers

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')
like image 153
jerryb Avatar answered Oct 21 '22 01:10

jerryb


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())
like image 1
yeugeniuss Avatar answered Oct 21 '22 01:10

yeugeniuss