I'm including some wordpress contents to my grails app with a customTag everything works fine.Now i want to render some standard-text if the url's status-code is not 200 OK
i have this so far
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
}
The correct url is commented out, i now expect the try to fail.
But instead of faling it's rendering some dns-error page of my provider.
So i think i have to look for http-status code, but how do i do that ?
for any hint, thanks in advance
You could try:
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"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With