[英]Groovy/Grails - get HttpResponseException by using HTTPBuilder
I try to get a data of Grails application (MyApplication2) from another Grails application (MyApplication2). 我尝试从另一个Grails应用程序(MyApplication2)获取Grails应用程序(MyApplication2)的数据。 Boths application run locally.
两者都在本地运行。 I get an exception thrown using the following code.
我使用以下代码引发异常。
Groovy 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()
}
Exception thrown 抛出异常
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.
I finally found the problem: I changed The URI
format. 我终于找到了问题:我更改了
URI
格式。 I added at the end of the URI
a /
, and I removed the /
at the beginning of the path
. 我在
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.