I’ve been wanting to add a custom User-Agent to groovyblogs.org for a while, so you know from your logs when I’m coming by to poll. It’s not easy to find sample code on this one, but the API does cover it just fine:

def client = new HttpClient()

def clientParams = client.getParams()
clientParams.setParameter(
    org.apache.commons.httpclient.params.HttpClientParams.USER_AGENT,
    "GroovyBlogs/1.0 (http://www.groovyblogs.org)")

If you need to enable your client for proxy support too, you’ll need to add a couple of more lines:

def hostConfig = client.getHostConfiguration()
hostConfig.setProxy("yourhost", 8080)

If it’s only proxy access you need, and you don’t need to mess with custom user agents or POSTed params, then I’ve found the simple URL class with some groovy enhancements is all I need: (just love that text property!)

System.properties["http.proxyHost"] = "yourhost";
System.properties["http.proxyPort"] = "8080";

def url = new URL("http://www.groovyblogs.org/")
println url.text

Happy proxying with your new User-Agent!