I wrote a builder for URLConnection, called URLConnectionBuilder. There is already HTTPBuilder for the similar purpose and it works great, but sometimes it's a little exaggerated because it depends on many other third-party modules. Also it has limit that it's only for http URL. URLConnectionBuilder only depends on standard modules and so it's easy to use. And of course you can use it for any URL.
Here is a simple example below:
----
import urlconnbuilder.URLConnectionBuilder;
def connBuilder = new URLConnectionBuilder()
connBuilder.url('http://groovy.codehaus.org/') {
connect {
configure (
requestProperties: [
'User-Agent': 'Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 5.1',
],
connectTimeout: 1000,
readTimeout: 1000
)
communicate (
input: { conn, stream ->
println stream.text
}
)
}
}
----
If you want to try it out, you can download it from here.
No comments:
Post a Comment