我在 Jenkinsfile 中有以下方法用于从给定的 URL 检索数据(将 json 发布到它并读取输出)。在 Jenkins 中调用它会导致构建以 *Proceeding.." 文本挂起。
@NonCPS
def callService(server, method, params = '') {
final HttpURLConnection connection = "http://$server:8080/router/".toURL().openConnection()
connection.setRequestMethod("POST");
connection.setRequestProperty('Accept', 'application/json;charset=utf-8')
connection.setRequestProperty('Content-Type', 'application/json;charset=utf-8')
connection.setDoOutput(true)
connection.outputStream.withWriter { Writer writer ->
writer << """{"jsonrpc": "2.0", "method": "$method", "params": {$params}}"""
}
String text = connection.inputStream.withReader { Reader reader -> reader.text }
return text
}
以及此方法的调用:
servers = ["example1"]
for (int i = 0; i < servers.size(); i++) {
server = servers[i]
assert callService(server, 'VersionService:getVersionDetails').matches('.*build:[1-9][0-9]*.*')
}
上面的代码是正确的,还是我做错了什么导致代码冻结?
当我使用 curl 做同样的事情时,它可以工作:
curl -v -H 'Accept: application/json;charset=utf-8' -H 'Content-Type: application/json;charset=utf-8' -d '{"jsonrpc": "2.0", "method":"VersionService:getVersionDetails", "params":{}}' http://example1:8080/router/