I need to check if a URL exists or returns 404 using Groovy Script. Is there any groovy api available for the same ? The corresponding java methods doesn't work with Groovy.
You can just do:
def code = new URL('http://www.google.com/made.up').openConnection().with {
requestMethod = 'HEAD'
connect()
responseCode
}
assert code == 404
Of course, if the domain doesn't exist, an Exception will be thrown
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