2014-07-01 38 views
0

我有下面的代碼在Groovy:如何使用Groovy/Java中的URL執行簡單請求?

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中有請求時,我看到一個結果。問題是我什麼都看不到。

可能是什麼問題?

回答

0

很可能,您的應用程序只會響應GET請求,而不會響應請求。嘗試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 
} 

此外,你確定你期待一個HTTP狀態201(創建)這個網址?

+0

謝謝你的狀態回調,但我繼續不與無響應 –

0

可以嘗試只打開一個簡單的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 
相關問題