class Owner {
static void main(String[] args) {
File file = new File('/Users/huleiwind/Downloads/app.txt')
file.eachLine {
def http = new HTTPBuilder("https://zappinfo.alipay.com")
http.request(GET, JSON) { req ->
headers.'User-Agent' = 'Mozilla/5.0'
uri.path = '/rest/app/queryBasic.json'
uri.query = [appName: it]
//请求成功
response.success = { resp, reader ->
assert resp.statusLine.statusCode == 200
println("${it} ${reader.app.owner.nickName} ${reader.app.bakOwner?.nickName}")
}
//404
response.'404' = { resp ->
println 'Not found'
}
// 401
http.handler.'401' = { resp ->
println "Access denied"
}
//其他错误,不实现则采用缺省的:抛出异常。
http.handler.failure = { resp ->
println "Unexpected failure: ${resp.statusLine}"
}
}
}
}
}groovy之json请求处理
最新推荐文章于 2025-08-07 14:04:13 发布
本文提供了一个使用Groovy编写的脚本示例,该脚本通过HTTP GET方式请求特定URL,并展示了如何处理响应结果。其中包括了如何设置User-Agent、指定路径及查询参数等细节。
1426

被折叠的 条评论
为什么被折叠?



