Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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(); 
like image 691
Ghanshyam Katriya Avatar asked Aug 05 '15 08:08

Ghanshyam Katriya


People also ask

What does URL openConnection do?

openConnection. Returns a URLConnection instance that represents a connection to the remote object referred to by the URL . A new instance of URLConnection is created every time when invoking the URLStreamHandler. openConnection(URL) method of the protocol handler for this URL.

Can I use Groovy in Java?

Groovy scripts can use any Java classes. They can be compiled to Java bytecode (in . class files) that can be invoked from normal Java classes. The Groovy compiler, groovyc, compiles both Groovy scripts and Java source files, however some Java syntax (such as nested classes) is not supported yet.


1 Answers

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

like image 128
Ghanshyam Katriya Avatar answered Nov 01 '22 09:11

Ghanshyam Katriya