srinath
srinath

Reputation: 399

Testing URLs in groovy

How can we check whether urls are working or not in groovy?
when we click a button, i will get all the urls from existing db from 'urls' table and need to check which url is working
Ex: http://baldwinfilter.com/products/start.html - not working
http://www.subaru.com/ - working
and so many urls from db.

My aim is to get all urls and check which one is working and which is not .
do we need to check on the status it returns ??
Can any one help me giving idea ...

thanks in advance sri...

Upvotes: 0

Views: 4832

Answers (1)

tim_yates
tim_yates

Reputation: 171054

You could use HttpBuilder like so:

@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.5.0-RC2' )
import groovyx.net.http.HTTPBuilder

def urls = [
  "http://baldwinfilter.com/products/start.html",
  "http://www.subaru.com/" 
]

def up = urls.collect { url ->
  try {
    new HTTPBuilder( url ).get( path:'' ) { response ->
      response.statusLine.statusCode == 200
    }
  }
  catch( e ) { false }
}
println up

Upvotes: 5

Related Questions