Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails/Groovy URL .getText receive status

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

like image 408
john Smith Avatar asked Nov 18 '13 13:11

john Smith


1 Answers

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"
}
like image 143
tim_yates Avatar answered Sep 19 '22 02:09

tim_yates