Startseite bisherige Projekte Tools/Snippets Bücherempfehlungen Publikationen Impressum Datenschutzerklärung

Proxy und JavaNovember 2013

Über den ProxySelector in Java eine Proxy-Auto-Config (PAC) Datei zu verwenden ist nicht trivial. Bibliotheken wie proxy-vole können helfen. Wenn ein festes JRE mit dem Programm zusammen ausgeliefert wird (Java 6 oder 7) lassen sich auch Klassen aus dem Paket deploy.jar verwenden, das im JRE/lib Verzeichnis liegt. Achtung, dass die richtige Version für 32bit/64bit verwendet wird.
Mit folgendem Schnipsel lassen sich die Einstellungen des Internet Explorers verwenden. Ist dort eine PAC-Datei eingetragen, wird diese auch von Java verwendet. Beim Ausprobieren daran denken, deploy.jar zum classpath hinzuzufügen.
Achtung: Da com.sun.*-Klassen verwendet werden, kann sich die Implementierung in zukünftigen Java-Versionen ändern.

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.SocketAddress;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.sun.deploy.net.proxy.AbstractAutoProxyHandler;
import com.sun.deploy.net.proxy.ProxyInfo;
import com.sun.deploy.net.proxy.WIExplorerAutoProxyHandler;
import com.sun.deploy.net.proxy.WIExplorerProxyConfig;

public class MyIESettingsProxy {

  public static void init() throws Exception {

    final AbstractAutoProxyHandler wHandler = getIEProxy();
    final ProxySelector psDefault = ProxySelector.getDefault();

    ProxySelector sel = new ProxySelector() {

      @Override
      public List<Proxy> select(URI uri){
        // Let's stick to the specs.
        if (uri == null) {
          throw new IllegalArgumentException("URI can't be null.");
        }

        List<Proxy> l = new ArrayList<Proxy>();
        
        try{
          if (wHandler != null) {
            ProxyInfo[] info = wHandler.getProxyInfo(uri.toURL());
            if (info != null && info.length > 0) {
              l.add(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(info[0].getProxy(), info[0].getPort())));
            }
            if (psDefault != null) {
              l.addAll(psDefault.select(uri));
            }
            l.add(Proxy.NO_PROXY);
            return l;
          }
        }
        catch (Exception e){
          e.printStackTrace();
        }
        // something went wrong, use default proxy
        l.add(Proxy.NO_PROXY);
        return l;
      }
      
      @Override
      public void connectFailed(URI arg0, SocketAddress arg1, IOException arg2) {
        // ignore
      }

    };

    ProxySelector.setDefault(sel);
  }

  private static AbstractAutoProxyHandler getIEProxy() throws Exception {
    WIExplorerProxyConfig configIE = new WIExplorerProxyConfig();
    if (configIE != null) {
      WIExplorerAutoProxyHandler wIE = new WIExplorerAutoProxyHandler();
      wIE.init(configIE.getBrowserProxyInfo());
      return wIE;
    }
    return null;
  }
  
  public static void main(String[] args) throws Exception{
    init();
    new URL("http://www.tweniger.de").openConnection().getInputStream().read();
  }
}
Impressum - Datenschutzerklärung