Skip to content

Instantly share code, notes, and snippets.

@michalbcz
Created November 29, 2012 09:58
Show Gist options
  • Save michalbcz/4167914 to your computer and use it in GitHub Desktop.
Save michalbcz/4167914 to your computer and use it in GitHub Desktop.
groovy - connect to URL through proxy
import java.net.*;
import java.io.*;
/* PROXY SETTINGS */
System.getProperties().put("proxySet", "true");
System.getProperties().put("proxyHost", "some.proxyserver.com");
System.getProperties().put("proxyPort", "8080");
Authenticator.setDefault(new MyAuthenticator());
URL url = new URL("http://www.google.com"); /* WHERE WE WANT TO CONNECT */
URLConnection conn = url.openConnection();
println url.text /* show content of target html response - throws exception when proxy or authentication doesn't work */
class MyAuthenticator extends Authenticator{
protected PasswordAuthentication getPasswordAuthentication(){
/* our proxy uses domain login */
return new PasswordAuthentication("domain\\username", "password".toCharArray()); /* CHANGE HERE */
}
}
@stepin
Copy link

stepin commented Nov 21, 2018

As alternative, it's possible to define proxy env variables on system level and specify "-Djava.net.useSystemProxies=true" for Groovy.
First line of Groovy script can be like this:

#!/usr/bin/env groovy -Djava.net.useSystemProxies=true

@shihai1991
Copy link

As alternative, it's possible to define proxy env variables on system level and specify "-Djava.net.useSystemProxies=true" for Groovy. First line of Groovy script can be like this:

#!/usr/bin/env groovy -Djava.net.useSystemProxies=true

It can not work in my centos :(

Caught: java.net.ConnectException: Connection refused (Connection refused)
java.net.ConnectException: Connection refused (Connection refused)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment