joh*_*ith 6 grails groovy http http-status-codes
我将一些wordpress内容包含在我的grails应用程序中,使用customTag一切正常.如果url的状态代码不是200,我想要呈现一些标准文本
到目前为止我有这个
def wordpressHp = { body ->
// def url = grailsApplication.config.wordpress.server.url+'?include=true'
def url = "http://www.dasdasdasgsdniga.nd"
def content
try{
content = url.toURL().getText(connectTimeout: 10000)
}catch(e){
content="SORRY WORDPRESS IS CURRENTLY NOT AVAILABLE"
}
out << content
}
Run Code Online (Sandbox Code Playgroud)
正确的url被注释掉了,我现在希望尝试失败.
但它不是渲染它渲染我的提供者的一些DNS错误页面.所以我认为我必须寻找http状态代码,但我该怎么做?
任何暗示,提前谢谢
tim*_*tes 11
你可以尝试:
def url = "http://www.dasdasdasgsdniga.nd"
def content
try {
content = url.toURL().openConnection().with { conn ->
readTimeout = 10000
if( responseCode != 200 ) {
throw new Exception( 'Not Ok' )
}
conn.content.withReader { r ->
r.text
}
}
}
catch( e ) {
content="SORRY WORDPRESS IS CURRENTLY NOT AVAILABLE"
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4533 次 |
| 最近记录: |