[英]Groovy/Grails - get HttpResponseException by using HTTPBuilder
我嘗試從另一個Grails應用程序(MyApplication2)獲取Grails應用程序(MyApplication2)的數據。 兩者都在本地運行。 我使用以下代碼引發異常。
Groovy
try {
def http = new HTTPBuilder('http://localhost:8080/MyApplication2')
http.get( path : '/ControllerName/Action', contentType : ContentType.TEXT) { resp, reader ->
println "response status: ${resp.statusLine}"
resp.headers.each { h ->
println " ${h.name} : ${h.value}"
}
}
} catch (groovyx.net.http.HttpResponseException ex) {
ex.printStackTrace()
println ex.toString()
} catch (java.net.ConnectException ex) {
ex.printStackTrace()
println ex.toString()
}
拋出異常
Error 2015-10-28 10:13:43,448 [http-bio-8090-exec-1] ERROR errors.GrailsExceptionResolver - HttpResponseException occurred when processing request: [GET] /MyApplication1/ControllerName/Action Not Found.
我終於找到了問題:我更改了URI
格式。 我在URI
的末尾添加了/
,並在path
的開頭刪除了/
。
try {
def http = new HTTPBuilder('http://localhost:8080/MyApplication2/')
http.get( path : 'ControllerName/Action', contentType : ContentType.TEXT) { resp, reader ->
println "response status: ${resp.statusLine}"
resp.headers.each { h ->
println " ${h.name} : ${h.value}"
}
}
} catch (groovyx.net.http.HttpResponseException ex) {
ex.printStackTrace()
println ex.toString()
} catch (java.net.ConnectException ex) {
ex.printStackTrace()
println ex.toString()
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.