问题描述
我听说Groovy有一个内置的REST / HTTP客户端。我能找到的唯一的图书馆是,是这样吗?
I heard that Groovy has a built-in REST/HTTP client. The only library I can find is HttpBuilder, is this it?
基本上,我正在寻找一种从Groovy代码中执行HTTP GET的方法,而无需导入任何库(如果可能的话)。但是,由于这个模块似乎不是核心Groovy的一部分,我不确定我是否有正确的lib。
Basically I'm looking for a way to do HTTP GETs from inside Groovy code without having to import any libraries (if at all possible). But since this module doesn't appear to be a part of core Groovy I'm not sure if I have the right lib here.
推荐答案
如果您的需求很简单,并且您希望避免添加其他依赖项,则可以使用Groovy添加到的
class: getText()
方法> java.net.URL
If your needs are simple and you want to avoid adding additional dependencies you may be able to use the getText()
methods that Groovy adds to the java.net.URL
class:
new URL("http://stackoverflow.com").getText()
// or
new URL("http://stackoverflow.com")
.getText(connectTimeout: 5000,
readTimeout: 10000,
useCaches: true,
allowUserInteraction: false,
requestProperties: ['Connection': 'close'])
如果您希望返回二进制数据,则也有类似的功能,它由 newInputStream()
方法提供。
If you are expecting binary data back there is also similar functionality provided by the newInputStream()
methods.
这篇关于Groovy内置的REST / HTTP客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!