Chris
Chris

Reputation: 3562

Groovy: Timeout due to proxy not being set (java.net.ConnectException)

I want to use a Groovy script to access a webpage. However I'm behind a proxy.

Here's a test script that fails...

println "Google page is..."
println 'http://www.google.com'.toURL().text

Here's the output...

>groovy proxytester.groovy 
Google page is... 
Caught: java.net.ConnectException: Connection timed out: connect
   at checker.run(proxytester.groovy:2)

How do I set proxy server information in Groovy?

Upvotes: 4

Views: 4941

Answers (2)

tim_yates
tim_yates

Reputation: 171054

Or, from inside Groovy itself:

System.properties << [ 'http.proxyHost':'proxyHost', 'http.proxyPort':'port' ]

Upvotes: 11

Chris
Chris

Reputation: 3562

The proxy information can be set for the JVM by passing arguments on the groovy command line, eg...

groovy -Dhttp.proxyHost=proxyHost -Dhttp.proxyPort=Port Number proxytester.groovy

This script then works...

println "Google page is..."
println 'http://www.google.com'.toURL().text

Here's the results...

Google page is
<!doctype html><html itemscope itemtype="http://schema.org/WebPage"><head><meta
http-equiv="content-type.....

See the Oracle docs for further information

Upvotes: 3

Related Questions