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 */ | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It can not work in my centos :(