Skip to content

Instantly share code, notes, and snippets.

@atbradley
Created March 22, 2012 20:02
Show Gist options
  • Save atbradley/2163004 to your computer and use it in GitHub Desktop.
Save atbradley/2163004 to your computer and use it in GitHub Desktop.
Disable automatic redirect handling in HTTPBuilder
// Return the actual location of a web page given a shortened url.
// Based in part on this Stack Overflow answer:
// http://stackoverflow.com/questions/1519392/how-to-prevent-apache-http-client-from-following-a-redirect
import groovyx.net.http.HTTPBuilder
import org.apache.http.impl.client.AbstractHttpClient
import org.apache.http.params.BasicHttpParams
import static groovyx.net.http.Method.HEAD
HTTPBuilder http = new HTTPBuilder(url)
AbstractHttpClient ahc = http.client
BasicHttpParams params = new BasicHttpParams();
params.setParameter("http.protocol.handle-redirects",false)
ahc.setParams(params)
http.request(HEAD) {
response.success = { rsp ->
println rsp.headers?.Location
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment