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
How to execute url in groovy?
提问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 params
value 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