Groovy 使用HTTP基本身份验证的XmlSlurper.parse(uri)
我需要从XML-RPC web服务中获取数据Groovy 使用HTTP基本身份验证的XmlSlurper.parse(uri),groovy,basic-authentication,xmlslurper,Groovy,Basic Authentication,Xmlslurper,我需要从XML-RPC web服务中获取数据 new XmlSlurper().parse(“http://host/service“”工作正常,但现在我有了一个需要基本HTTP身份验证的特定服务 如何为parse()方法设置用户名和密码,或修改请求的HTTP头 使用http://username:password@主机/服务不起作用-我仍然得到java.io.IOException:服务器返回的HTTP响应代码:401表示URL异常 谢谢我发现哪种可能有用 根据您的情况编辑此代码,我们得到:
new XmlSlurper().parse(“http://host/service“”
工作正常,但现在我有了一个需要基本HTTP身份验证的特定服务
如何为parse()
方法设置用户名和密码,或修改请求的HTTP头
使用http://username:password@主机/服务
不起作用-我仍然得到java.io.IOException:服务器返回的HTTP响应代码:401表示URL
异常
谢谢我发现哪种可能有用
根据您的情况编辑此代码,我们得到:
def addr = "http://host/service"
def authString = "username:password".getBytes().encodeBase64().toString()
def conn = addr.toURL().openConnection()
conn.setRequestProperty( "Authorization", "Basic ${authString}" )
if( conn.responseCode == 200 ) {
def feed = new XmlSlurper().parseText( conn.content.text )
// Work with the xml document
} else {
println "Something bad happened."
println "${conn.responseCode}: ${conn.responseMessage}"
}
这对你有用
请记住使用此选项而不是上面提到的“def authString”:
def authString = "${usr}:${pwd}".getBytes().encodeBase64().toString()
我将参数定义为usr和pwd。干杯