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();
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.
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.
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
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