21

当使用带有基本身份验证的 groovy 的 http-builder 时,默认行为是首先发送未经身份验证的请求,并在收到 401 后重新发送带有凭据的请求。Apache 的 Httpclient 提供抢先式身份验证以在第一个请求时直接发送凭据。如何在 Groovy 的 http-builder 中使用抢先式身份验证?任何代码示例表示赞赏。

4

2 回答 2

37

你也可以用 groovy 风格解决它

http = new RESTClient('http://awesomeUrl/')
http.headers['Authorization'] = 'Basic '+"myUsername:myPassword".getBytes('iso-8859-1').encodeBase64()
于 2013-03-07T20:16:59.263 回答
34

基于JIRA 问题,您可以执行以下操作:

def http = new RESTClient('http://awesomeUrl/')

http.client.addRequestInterceptor(new HttpRequestInterceptor() {
    void process(HttpRequest httpRequest, HttpContext httpContext) {
        httpRequest.addHeader('Authorization', 'Basic ' + 'myUsername:myPassword'.bytes.encodeBase64().toString())
    }
})

def response = http.get(path: "aResource")

println response.data.text
于 2011-07-10T15:38:29.193 回答