Testing URLs in groovy
How can we check whether urls are working or not in groovy?
when we click a 开发者_如何学Cbutton, 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...
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
精彩评论