Skip to content

Instantly share code, notes, and snippets.

@axeda
Last active June 28, 2022 19:08
Show Gist options
  • Save axeda/5189102 to your computer and use it in GitHub Desktop.
Save axeda/5189102 to your computer and use it in GitHub Desktop.
Connection Timeouts with HttpBuilder
import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.*
int TENSECONDS = 10*1000;
int THIRTYSECONDS = 30*1000;
HTTPBuilder builder = new HTTPBuilder('http://www.axeda.com')
//HTTPBuilder has no direct methods to add timeouts. We have to add them to the HttpParams of the underlying HttpClient
builder.getClient().getParams().setParameter("http.connection.timeout", new Integer(TENSECONDS))
builder.getClient().getParams().setParameter("http.socket.timeout", new Integer(THIRTYSECONDS))
try
{
//Simply get the contents of http://www.axeda.com and log it to the Custom Object Log
builder.request(GET, TEXT){
response.success = { resp, res ->
res.readLines().each {
logger.info it
}
}
}
}
finally
{
//Make sure to always shut down the HTTPBuilder when you’re done with it
builder.shutdown()
}
return true
@pablovilas
Copy link

Great, thanks!.

@jeremyjjbrown
Copy link

Nice, Thanks.

@SlavikCA
Copy link

HttpParams getParams();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment