Groovy in Jenkinsfile hangs when posting to an URL
<p>I have following method in Jenkinsfile used to retrieve data from given URL (posting json to it and reading the output).
Calling it in Jenkins causes the build to hang with *Proceeding.." text.</p>

<pre><code>@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 -&gt;                       
        writer &lt;&lt; """{"jsonrpc": "2.0", "method": "$method", "params": {$params}}"""
    }                                                                           
    String text = connection.inputStream.withReader { Reader reader -&gt; reader.text }
    return text                                                                                                                                                                               
}                                                                               
</code></pre>

<p>And the call of this method:</p>

<pre><code>servers = ["example1"]
for (int i = 0; i &lt; servers.size(); i++) {                                  
    server = servers[i]                                                     
    assert callService(server, 'VersionService:getVersionDetails').matches('.*build:[1-9][0-9]*.*') 
}   
</code></pre>

<p>Is the above code correct groovy one, or am I doing something wrong that causes the code to freeze?</p>

<p>When I do the same thing using curl, it works:</p>

<pre><code>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/
</code></pre>