使用Groovy HTTP的建设者preemptive模式 [英] using groovy http-builder in preemptive mode

查看:210
本文介绍了使用Groovy HTTP的建设者preemptive模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用Groovy的HTTP的建设者基本身份验证的默认行为是在第一时间接收到401之后,首先发送一个未经身份验证的请求并重新递交这个请求。
Apache的HttpClient的报价<一个href=\"http://hc.apache.org/httpclient-3.x/authentication.html#$p$pemptive_Authentication\">$p$pemptive验证的直接发送的第一个请求的凭据。
如何使用preemptive权威性在Groovy的HTTP建设者?任何code例子是AP preciated。

When using groovy's http-builder with basic authentication the default behavior is to send an unauthenticated request first and resend the request with credentials after receiving a 401 in the first place. Apache's Httpclient offers preemptive authentication to send the credentials directly on the first request. How can I use preemptive auth in Groovy's http-builder? Any code examples are appreciated.

推荐答案

根据一个的 JIRA问题你可以做这样的事情:

Based on a JIRA issue you can do something like that :

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

这篇关于使用Groovy HTTP的建设者preemptive模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆