下面的代码不验证用户(无身份验证失败情况发生,但调用失败,由于缺乏权限):为什么HTTPBuilder基本身份验证不起作用?
def remote = new HTTPBuilder("http://example.com")
remote.auth.basic('username', 'password')
remote.request(POST) { req ->
uri.path = "/do-something"
uri.query = ['with': "data"]
response.success = { resp, json ->
json ?: [:]
}
}
但以下工作正常:
def remote = new HTTPBuilder("http://example.com")
remote.request(POST) { req ->
uri.path = "/do-something"
uri.query = ['with': "data"]
headers.'Authorization' =
"Basic ${"username:password".bytes.encodeBase64().toString()}"
response.success = { resp, json ->
json ?: [:]
}
}
为什么ISN第一个工作吗?
它是如何失败?服务器应该返回一个HTTP 401状态码来触发基本认证。然后HttpBuilder将发送第二个请求和授权标头。 – ataylor
它只是不起作用。请求本身会返回一条消息,指出用户没有执行该操作的权限。我可以将用户名和密码更改为完全无效的内容,并发生相同的情况。 –
这应该有可能解决您的问题 http://stackoverflow.com/questions/6588256/using-groovy-http-builder-in-preemptive-mode –