How to use automatic proxy script configuration in Java

My Internet Explorer has an automatic proxy file (called a PAC) for accessing the network. Is there a way to use this in my Java program, too?

My below Java code does not seem to use proxies at all.

ArrayList<Proxy> ar = new ArrayList<Proxy>(ProxySelector.getDefault().select(new URI("http://service.myurlforproxy.com")));
for(Proxy p : ar){
  System.out.println(p.toString()); //output is just DIRECT T.T it should be PROXY.
}

I also install my proxy script in the Java control panel (Control-> Java), but the same result. and I did not find there any way to install the PAC program file for Java programmatically.

People use http.proxyHost for System.setProperties (..), but this is only for setting a proxy host, not a proxy script file (PAC file).

+8
source share
4 answers

! Proxy Auto-Config (PAC) Java. . .

import com.sun.deploy.net.proxy.*;
.
.
BrowserProxyInfo b = new BrowserProxyInfo();        
b.setType(ProxyType.AUTO);
b.setAutoConfigURL("http://yourhost/proxy.file.pac");       
DummyAutoProxyHandler handler = new DummyAutoProxyHandler();
handler.init(b);

URL url = new URL("http://host_to_query");
ProxyInfo[] ps = handler.getProxyInfo(url);     
for(ProxyInfo p : ps){
    System.out.println(p.toString());
}

[com.sun.deploy.net.proxy] ! [deploy.jar]; D

+9

Java JS PAC. . , -. .

+2

Based on @Jaeh's answer, I used the code below. Note: SunAutoProxyHandler implements AbstractAutoProxyHandler , and there is an alternative concrete implementation called PluginAutoProxyHandler , but this implementation does not look as reliable:

    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());
    }
+1
source

In my case, I just realized that it would return a .pac file and then hardcode.

0
source

Source: https://habr.com/ru/post/1531953/


All Articles