Ghanshyam Katriya
Ghanshyam Katriya

Reputation: 1081

How to execute url in groovy?

I want to call a with the format http://x.x.x.x/test/test.jsp?[params] in Groovy. In this file I am getting params value from the URL for further processing. However, I want to know how to call this URL from Groovy in the first place.

I tried this bit it didn't work: (I am new to Groovy, to be fair.)

URL url = new URL("http://192.168.1.87:8080/bridge/test.php");
URLConnection conn = url.openConnection(); 

Upvotes: 7

Views: 30012

Answers (1)

Ghanshyam Katriya
Ghanshyam Katriya

Reputation: 1081

This code works for me :

def url = new URL("http://X.X.X.X:8080/url?[params]")
HttpURLConnection connection = (HttpURLConnection) url.openConnection()
connection.setRequestMethod("GET")
// connection.setConnectTimeout(10000)
connection.connect()
if (connection.responseCode == 200 || connection.responseCode == 201) {
    def returnMessage = connection.content
} else {
}

Refrence : Connection timeout with HttpURLConnection in Groovy

Upvotes: 8

Related Questions