经典 Groovy HTTPBuilder 的认证代理设置
为代理的主机和端口配置经典 HTTPBuilder 路由及其 Apache 凭据提供程序。
对于使用经典 groovyx.net.http.HTTPBuilder 的应用程序,在构建器上设置路由,并在其 Apache HTTP 客户端的凭据提供程序中设置代理账户:
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)
)
凭据变量来自应用程序。AuthScope 与紧接其上方配置的代理主机和端口相匹配。之后通过该构建器发出的请求都会使用这个已配置的客户端。
经典 HTTPBuilder 的默认实现会创建一个 Apache DefaultHttpClient,其公开方法 setProxy 会设置该客户端的默认代理路由。即使应用程序本身运行在 Grails 或 Tomcat 中,这也是相关的配置对象。
示例针对的正是这套经典 API。HTTPBuilder-NG 的配置模型不同,替换经典构建器底层客户端的应用程序必须使用替换实现所支持的接口。从应用程序配置中加载时,请把主机名、整数端口和代理方案保留为各自独立的值。