java 如何在groovy中执行url?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31827135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 19:11:43  来源:igfitidea点击:

How to execute url in groovy?

javagrailsgroovy

提问by Ghanshyam Katriya

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 paramsvalue from the URL for further processing. However, I want to know how to call this URL from Groovy in the first place.

我想用http://x.x.x.x/test/test.jsp?[params]Groovy 中的格式调用 a 。在此文件中,我params从 URL获取值以进行进一步处理。但是,我首先想知道如何从 Groovy 调用此 URL。

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

我尝试了这一点,但没有用:(公平地说,我Groovy 的新手。)

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

回答by Ghanshyam Katriya

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

参考:Groovy 中 HttpURLConnection 的连接超时