Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use automatic proxy configuration script in Java

Tags:

java

proxy

pac

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 does not seem to use proxy 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 set my proxy script on Java Control Panel(Control->Java), but the same result. and I found there's no way to set PAC file for Java programmatically.

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

like image 950
Jaeh Avatar asked Jun 07 '12 16:06

Jaeh


People also ask

How do I use automatic configuration script proxy?

On the Tools menu, click Internet Options, and then click Connections. Click Settings or LAN Settings. In the Automatic configuration area, check that you've chosen the Use automatic configuration script box, and that it has the correct location to your automatic configuration script or for your automatic proxy URL.

How do I configure proxy settings for Java?

Configure Proxies through the Java Control PanelIn the Java Control Panel, under the General tab, click on Network Settings. Select the Use Browser Settings checkbox. Click OK to save your changes. Close all browser windows.

Does Java use Http_proxy?

Java provides proxy handlers for HTTP, HTTPS, FTP, and SOCKS protocols. A proxy can be defined for each handler as a hostname and port number: http.


1 Answers

Wow! I could load Proxy Auto-Config (PAC) file on Java. Please see below codes and package.

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

You already have a [com.sun.deploy.net.proxy] package on your machine! Find [deploy.jar] ;D

like image 147
Jaeh Avatar answered Oct 13 '22 17:10

Jaeh