Created
November 29, 2012 09:58
-
-
Save michalbcz/4167914 to your computer and use it in GitHub Desktop.
groovy - connect to URL through proxy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ | |
} | |
} | |
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
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: