tags:

views:

225

answers:

1

I use the following Groovy snippet to obtain the plain-text representation of an HTML-page in a Grails application:

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

Now I want to alter the code so that the request will timeout after 5 seconds (resulting instr == null). What is the easiest and most Groovy way to achieve that?

+2  A: 

You'd have to do it the old way, getting a URLConnection, setting the timeout on that object, then reading in the data through a Reader

This would be a good thing to add to Groovy though (imho), as it's something I could see myself needing at some point ;-)

Maybe suggest it as a feature request on the JIRA?

I've added it as a RFE on the Groovy JIRA

http://jira.codehaus.org/browse/GROOVY-3921

So hopefully we'll see it in a future version of Groovy...

tim_yates
+1: Very nice! And with a patch: http://jira.codehaus.org/secure/attachment/46210/URL.text-timout.patch
knorv
Wish I could think of a way of testing though (without requiring the test machine to have an internet connection, and without hammering some poor site each time someone runs the test) ;-)
tim_yates