9

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.

tim_yates
  • 167,322
  • 27
  • 342
  • 338
Steve s.
  • 301
  • 1
  • 4
  • 13

4 Answers4

7

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')
jerryb
  • 1,425
  • 1
  • 9
  • 11
1

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())
yeugeniuss
  • 180
  • 1
  • 7
0

From the JavaDoc, it appears you do it by using AsyncHttpBuilder instead? That class extends HTTPBuilder and it has a setTimeout(int) method.

Look at this: http://www.kellyrob99.com/blog/2013/02/10/groovy-and-http/ According to that, it appears you might be able to follow the advice here to get a timeout on your connection: Java HTTP Client Request with defined timeout

Community
  • 1
  • 1
djangofan
  • 28,471
  • 61
  • 196
  • 289
  • Thanks, good idea. However, I looked at the code for AsyncHTTPBuilder. It also uses the deprecated method of setting params. I guess there's no better way. – Steve s. Feb 25 '16 at 23:09
  • Ok, i added a url to my answer. Maybe that will help. – djangofan Feb 25 '16 at 23:31
  • Thanks again, but the issue isn't about getting it to work - the timeout setting is fine. The issue is that AsyncHTTPBuilder is using deprecated methods. Check out line 211 [code](https://github.com/jgritman/httpbuilder/blob/master/src/main/java/groovyx/net/http/AsyncHTTPBuilder.java) . client.getParams() is deprecated. – Steve s. Feb 25 '16 at 23:49
  • I sorta see what you mean here: https://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/params/HttpConnectionParams.html – djangofan Feb 25 '16 at 23:53
  • Yeah - you got it, djangofan. Actually, I'm getting concerned as it seems HTTPBuilder might be a dead project. – Steve s. Feb 26 '16 at 00:25
  • I'm surprised the latest Groovy still uses that library. Maybe there is a reason for it. NO answer was given to this question: https://github.com/jgritman/httpbuilder/issues/57 – djangofan Feb 26 '16 at 15:54
  • It appears you MIGHT be able to get around the deprecation by using this method? http://stackoverflow.com/questions/15336477/deprecated-java-httpclient-how-hard-can-it-be – djangofan Feb 26 '16 at 15:58
-1

I use like this

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))
jpozorio
  • 622
  • 6
  • 7