Skip to content

Instantly share code, notes, and snippets.

@bryanchug
Created November 29, 2012 23:20
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bryanchug/4172603 to your computer and use it in GitHub Desktop.
Save bryanchug/4172603 to your computer and use it in GitHub Desktop.
HTTPBuilder with Proxy
@Grapes(
@Grab(group='org.codehaus.groovy', module='http-builder', version='0.4.1')
)
import groovyx.net.http.*
import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.*
import org.apache.http.auth.*
def http = new HTTPBuilder( 'http://www.ipchicken.com' )
http.client.getCredentialsProvider().setCredentials(
new AuthScope("myproxy.com", 8080),
new UsernamePasswordCredentials("proxy-username", "proxy-password")
)
http.setProxy('myproxy.com', 8080, 'http')
http.request( GET, TEXT ){ req ->
response.success = { resp, reader ->
println "Response: ${reader.text}"
}
}
@thekalinga
Copy link

Any idea why the proxy details are given more than once?

@patricematignon
Copy link

The first time is to associate the credentials with that url (most likely the proxy will send a 401 which can be automatically responded to with the stored credentials). The second time to declare the proxy coordinates itself.

@yellek
Copy link

yellek commented Mar 10, 2016

Why can't the proxy settings default to the Java system property values? I don't want to be changing my code based on whether or not I am running behind a proxy.

@Ascell
Copy link

Ascell commented Sep 9, 2016

will it work in conjunction with my provider advanced.name/ru ?

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