Java HTTP Proxy Authentication

When writing Java programs there is sometimes a need to get content from the Internet. However, to do that from within the ECS network requires authenticating with the HTTP proxy server. Below are the ECS proxy settings and a Java class wrapper to provide this functionality.

More information at Java Proxy Configuration.

ECS Proxy Settings

The ECS proxy is www-cache.ecs.vuw.ac.nz or 130.195.5.7 and uses port 8080.

See http://ecs.victoria.ac.nz/Support/TechNoteWebBrowserSetup for more information about setting your web browser proxy settings.

ECS Proxy Authenticator

Thanks to HughDavenport and Kyle Chard for this Java code that works with the ECS proxy.

Example use:
  • Command Line: java ECSAuthenticator Foo (where Foo is the name of the class you want to execute)
  • Eclipse: Inside Run Configurations, and "Main" tab set the "Main Class" option as ECSAuthenticator then in "Arguments" tab set the "Program arguments" option as Foo (name of class to execute).

public class ECSAuthenticator extends java.net.Authenticator {
   protected java.net.PasswordAuthentication getPasswordAuthentication() {
      return new java.net.PasswordAuthentication("username", "password".toCharArray());
   }

   public static void main(String[] args) throws Exception {
      java.net.Authenticator.setDefault(new ECSAuthenticator());
      System.setProperty("http.proxyHost", "www-cache.ecs.vuw.ac.nz");
      System.setProperty("http.proxyPort", "8080");
      System.setProperty("http.nonProxyHosts", "localhost|*.vuw.ac.nz|*.victoria.ac.nz|*.elvis.ac.nz");
      
      String[] newArgs = new String[args.length-1];
      System.arraycopy(args, 1, newArgs, 0, args.length-1);
      Class[] methodTypes = new Class[1];
      methodTypes[0] = newArgs.getClass();
      Object[] methodArgs = new Object[1];
      methodArgs[0] = newArgs;
      Class mainClass = Class.forName(args[0]);
      java.lang.reflect.Method mainMethod  = mainClass.getMethod("main", methodTypes);
      mainMethod.invoke(null, methodArgs);

   }
}