For an application using the classic groovyx.net.http.HTTPBuilder, set the route on the builder and the proxy account on its Apache HTTP client's credentials provider:

import groovyx.net.http.HTTPBuilder
import org.apache.http.auth.AuthScope
import org.apache.http.auth.UsernamePasswordCredentials

def http = new HTTPBuilder('https://api.example.net')
http.setProxy('proxy.example.net', 8080, 'http')

http.client.credentialsProvider.setCredentials(
    new AuthScope('proxy.example.net', 8080),
    new UsernamePasswordCredentials(proxyUser, proxyPassword)
)

The credential variables come from the application. The AuthScope matches the proxy host and port configured immediately above it. Subsequent requests made through this builder use that configured client.

Classic HTTPBuilder's default implementation creates an Apache DefaultHttpClient, and its public setProxy method sets that client's default proxy route. This is the relevant configuration object even when the application itself runs inside Grails or Tomcat.

The example targets that classic API. HTTPBuilder-NG has a different configuration model, and an application that replaces the classic builder's underlying client must use interfaces supported by the replacement. Keep the hostname, integer port and proxy scheme as separate values when loading them from application configuration.

Tënë sô zo