How to use automatic proxy configuration script in Java

前端 未结 4 1549
囚心锁ツ
囚心锁ツ 2021-01-02 05:49

My Internet Explorer is set to have an automatic proxy file(so-called PAC) for web access. Is there a way to use this on my Java program, also ?

My below Java code

4条回答
  •  渐次进展
    2021-01-02 06:19

    Based on @Jaeh answer I used the code below. Note that SunAutoProxyHandler implements AbstractAutoProxyHandler and there is an alternative concrete implementation called PluginAutoProxyHandler but that implementation does not appear to be as robust:

        BrowserProxyInfo b = new BrowserProxyInfo();
        b.setType(ProxyType.AUTO);
        b.setAutoConfigURL("http://example.com/proxy.pac");
    
        SunAutoProxyHandler handler = new SunAutoProxyHandler();
        handler.init(b);
    
        ProxyInfo[] ps = handler.getProxyInfo(new URL(url));
        for(ProxyInfo p : ps){
            System.out.println(p.toString());
        }
    

提交回复
热议问题