0

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...

1 Answer 1

5

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
17
  • Hi tim yates, I was getting erors with first line says "unexpected token:import @line 2, column 1. i tried removing first line and deployed but it shows "org.codehaus.groovy.runtime.InvokerInvocationException: java.lang.NoClassDefFoundError: groovyx.net.http.HTTPBuilder". Can you please tell me to include any jars ??
    – srinath
    Mar 15, 2010 at 9:32
  • Sounds like the Grab issue in Groovy version 1.6. Can you try with 1.7.1? Looking up the download URLs as I type this, but the site's down :-(
    – tim_yates
    Mar 15, 2010 at 10:11
  • Oh god, checked in ubuntu synaptic shows version groovy 1.5.7.1, I should update to latest version 1.7.1 ?
    – srinath
    Mar 15, 2010 at 10:37
  • 1.5 is kinda old. Incase you need it though, the http-builder can be downloaded form here: repository.codehaus.org/org/codehaus/groovy/modules/…
    – tim_yates
    Mar 15, 2010 at 10:44
  • thanks tim, i will try with this, if not anyways will upgrade to 1.7.1 .so that Grab will come default with 1.7.1. I will be in touch with you .
    – srinath
    Mar 15, 2010 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.