如何在Groovy/Java中使用URL执行简单请求?
我在groovy中有以下代码:如何在Groovy/Java中使用URL执行简单请求?,groovy,httpbuilder,Groovy,Httpbuilder,我在groovy中有以下代码: def http = new HTTPBuilder( 'http://localhost:8080' ) http.post( path: '/this/is/my/path/'+variable) { resp -> println "POST Success: ${resp.statusLine}" assert resp.statusLine.statusCode == 200 } 我只想执行那个请求。我在另一个应用程序中有一个方法,当
def http = new HTTPBuilder( 'http://localhost:8080' )
http.post( path: '/this/is/my/path/'+variable) { resp ->
println "POST Success: ${resp.statusLine}"
assert resp.statusLine.statusCode == 200
}
我只想执行那个请求。我在另一个应用程序中有一个方法,当url中有请求时,我会看到结果。问题是我什么也看不见
可能是什么问题?很可能,您的应用程序只响应
GET
请求,而不响应POST
请求。请尝试GET
:
def http = new HTTPBuilder( 'http://localhost:8080' )
http.get( path: '/this/is/my/path/'+variable) { resp ->
println "GET Success: ${resp.statusLine}"
assert resp.statusLine.statusCode == 200
}
另外,您确定您希望此URL上出现HTTP状态201(已创建)?可以尝试打开一个简单的
HttpURLConnection
,如下所示:
URL url = new URL("http://localhost:8080/this/is/my/path/${variable}")
HttpURLConnection connection = url.openConnection()
println "responseCode: ${connection.responseCode}"
assert connection.responseCode == 200
感谢您的状态更正,但我继续使用get,没有任何响应