设置新URL(…)的超时。Groovy/Grails中的文本

设置新URL(…)的超时。Groovy/Grails中的文本,grails,groovy,Grails,Groovy,我使用以下Groovy代码片段获取Grails应用程序中HTML页面的纯文本表示: String str = new URL("http://www.example.com/some/path")?.text?.decodeHTML() 现在我想修改代码,使请求在5秒后超时(结果是str==null)。要实现这一点,最简单、最常规的方法是什么?您必须使用旧方法,获取URLConnection,设置该对象的超时,然后通过读取器读取数据 这将是一个很好的东西添加到Groovy(imho),因为我可

我使用以下Groovy代码片段获取Grails应用程序中HTML页面的纯文本表示:

String str = new URL("http://www.example.com/some/path")?.text?.decodeHTML()

现在我想修改代码,使请求在5秒后超时(结果是
str==null
)。要实现这一点,最简单、最常规的方法是什么?

您必须使用旧方法,获取URLConnection,设置该对象的超时,然后通过读取器读取数据

这将是一个很好的东西添加到Groovy(imho),因为我可以看到自己在某个时候需要它;-)

也许建议将其作为JIRA上的功能请求

我把它作为RFE添加到Groovy JIRA上


所以希望我们能在Groovy的未来版本中看到它…

我检查了Groovy的源代码
2.1.8
,下面的代码是可用的:

'http://www.google.com'.toURL().getText([connectTimeout: 2000, readTimeout: 3000])

处理配置映射的逻辑位于方法
org.codehaus.groovy.runtime.ResourceGroovyMethods\configuredInputStream

private static InputStream configuredInputStream(Map parameters, URL url) throws IOException {
    final URLConnection connection = url.openConnection();
    if (parameters != null) {
        if (parameters.containsKey("connectTimeout")) {
            connection.setConnectTimeout(DefaultGroovyMethods.asType(parameters.get("connectTimeout"), Integer.class));
        }
        if (parameters.containsKey("readTimeout")) {
            connection.setReadTimeout(DefaultGroovyMethods.asType(parameters.get("readTimeout"), Integer.class));
        }
        if (parameters.containsKey("useCaches")) {
            connection.setUseCaches(DefaultGroovyMethods.asType(parameters.get("useCaches"), Boolean.class));
        }
        if (parameters.containsKey("allowUserInteraction")) {
            connection.setAllowUserInteraction(DefaultGroovyMethods.asType(parameters.get("allowUserInteraction"), Boolean.class));
        }
        if (parameters.containsKey("requestProperties")) {
            @SuppressWarnings("unchecked")
            Map<String, String> properties = (Map<String, String>) parameters.get("requestProperties");
            for (Map.Entry<String, String> entry : properties.entrySet()) {
                connection.setRequestProperty(entry.getKey(), entry.getValue());
            }
        }

    }
    return connection.getInputStream();
}
私有静态InputStream configuredInputStream(映射参数、URL URL)引发IOException{
final URLConnection connection=url.openConnection();
if(参数!=null){
if(parameters.containsKey(“connectTimeout”)){
setConnectTimeout(DefaultGroovyMethods.asType(parameters.get(“connectTimeout”),Integer.class));
}
if(parameters.containsKey(“readTimeout”)){
setReadTimeout(DefaultGroovyMethods.asType(parameters.get(“readTimeout”),Integer.class));
}
if(parameters.containsKey(“useCaches”)){
setUseCaches(DefaultGroovyMethods.asType(parameters.get(“useCaches”),Boolean.class));
}
if(parameters.containsKey(“allowUserInteraction”)){
setAllowUserInteraction(DefaultGroovyMethods.asType(parameters.get(“allowUserInteraction”)、Boolean.class));
}
if(parameters.containsKey(“requestProperties”)){
@抑制警告(“未选中”)
Map properties=(Map)parameters.get(“requestProperties”);
对于(Map.Entry:properties.entrySet()){
setRequestProperty(entry.getKey(),entry.getValue());
}
}
}
返回连接。getInputStream();
}

+1:非常好!还有一个补丁:希望我能想出一种测试的方法(不需要测试机器有互联网连接,也不需要每次有人运行测试时敲打一些糟糕的站点);-)只是一个更新,它已在Groovy中发布。返回新的URL(URL).getText([connectTimeout:500,readTimeout:5000]);