3

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.

1 Answer 1

6

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

Sign up to request clarification or add additional context in comments.

2 Comments

I am getting the folllowing error for the above code. JBO-25152: Calling the constructor for class java.net.URL is not permitted.
so is this in Jenkins, or some kind of container?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.