mku*_*min 10 groovy xmlslurper basic-authentication
我需要从XML-RPC Web服务中获取数据.
new XmlSlurper().parse("http://host/service") 工作正常,但现在我有一个特殊的服务,需要基本的HTTP身份验证.
如何为parse()方法设置用户名和密码,或修改请求的HTTP标头?
使用http://username:password@host/service没有帮助 - 我仍然得到java.io.IOException: Server returned HTTP response code: 401 for URL例外.
谢谢
tim*_*tes 19
根据您的情况编辑此代码,我们得到:
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}"
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11214 次 |
| 最近记录: |