Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Virtual Machine / April 2005

Tip: Looking for answers? Try searching our database.

Getting IE automated proxy settings from Java or JScript?

Thread view: 
Chris Frederick - 25 May 2004 15:23 GMT
I've been researching this on and off for weeks, and haven't come up with
anything useful yet.  If anyone knows how to do this, please let me know.
From a Java applet running in IE 6.0 using the Sun J2SE 1.4.2_03 plug-in, I
need to retrieve the proxy host and port that will be used to access a
specific URL.  The site with the client machines is using automated proxy
settings (i.e., a ".pac" file) to retrieve the proxy server address and port
number.

So basically, I need to do one of two things:

1.  From either Java or Javascript/Jscript, find the URL to the PAC file
that is stored in the browser configuration.  Open that URL, and execute the
FindProxyForURL() Javascript function therein.
2.  Even better, find a way to access the copy of FindProxyForURL() that
Sun's documentation claims the Java Plug-in preloads when it starts.  See
the following URL:
http://java.sun.com/j2se/1.4.2/docs/guide/plugin/developer_guide/proxie_config.h
tml#automatic


The restrictions on my environment are:

1.  I cannot use any native Windows code, whether called via JNI or exec(),
to read the registry value that contains the URL of the .pac file.
2.  I cannot hard code the auto configuration URL, because remote
administration software can change it.
3.  I cannot modify the Java code to use the URLConnection class and let the
JRE transparently handle the proxying.

The security permissions in the JRE's java.policy file may be relaxed,
within reason, to allow the applet to read (but not write) certain system
properties that are not readable by default.  This is how I get the IE proxy
settings when it is NOT configured to use a .pac file, but as far as I can
tell, there is no Java property that provides the automated proxy URL if the
browser is configured to use one.

Thanks in advance for any advice that you can provide.

- Chris
Chris - 03 Jun 2004 08:30 GMT
In case anyone else needs to do this, here's what I came up with.  There
still doesn't appear to be a good way to get the desired information using
J2SE 1.4.2.  However, it's trivial with the upcoming J2SE 1.5.0 (which is
currently available in beta), using the new ProxySelector class and
associated APIs.

ProxySelector ps = ProxySelector.getDefault();
URI uri = null;
try {
  uri = new URI("https://the_site_i_need_to.access.com");
}
catch (URISyntaxException e) {
 // Handle exception
}
List proxyList = ps.select(uri);
int len = proxyList.size();
for (int i = 0; i < len; i++) {
 Proxy p = (Proxy) proxyList.get(i);
 InetSocketAddress addr = (InetSocketAddress) p.address();
 if (addr == null) {
   // Use a direct connection - no proxy
 }
 else {
   InetAddress ip_addr = addr.getAddress();
   int tcp_port = addr.getPort();
   // Use the specified IP and port number as the proxy
 }
}

> I've been researching this on and off for weeks, and haven't come up with
> anything useful yet.  If anyone knows how to do this, please let me know.
[quoted text clipped - 12 lines]
> Sun's documentation claims the Java Plug-in preloads when it starts.  See
> the following URL:

http://java.sun.com/j2se/1.4.2/docs/guide/plugin/developer_guide/proxie_config.h
tml#automatic


> The restrictions on my environment are:
>
[quoted text clipped - 15 lines]
>
> - Chris
Martin  Adler - 16 Apr 2005 12:36 GMT
My actual snippet for 1.3,1.4,1.5 using java reflection so should compile
in all releases......:

public static String prepareProxy(String inurl,Properties outP) {
       String sProxyHost = null;
        int nProxyPort = -1;
       String result = "NOPROXY";
        String sJavaVer = System.getProperty("java.version");

        if(sJavaVer.startsWith("1.3")) {// if java plugin 1.3
            // full dynamic?!?!?!
            try {
                // proxyHandler =
sun.plugin.protocol.PluginProxyHandler.getDefaultProxyHandler();
                Class PluginProxyHandler = Class.forName
("sun.plugin.protocol.PluginProxyHandler");
                Method getDefaultProxyHandler = PluginProxyHandler.getMethod
("getDefaultProxyHandler", new Class[] { });
                Object proxyHandler = getDefaultProxyHandler.invoke(null, new Object[]
{ });

                // proxyInfo = proxyHandler.getProxyInfo(url);
                Class ProxyHandler = Class.forName("sun.plugin.protocol.ProxyHandler");
                Method getProxyInfo = ProxyHandler.getMethod("getProxyInfo", new Class[]
{ java.net.URL.class });
                Object proxyInfo = getProxyInfo.invoke(proxyHandler, new Object[] { new
java.net.URL(inurl) });

                // need this for method invocation!!!!
                Class ProxyInfo = Class.forName("sun.plugin.protocol.ProxyInfo");
                // if (proxyInfo.isSocksUsed()) {
                if (((Boolean)ProxyInfo.getMethod("isSocksUsed", new Class[] { })
.invoke(proxyInfo, new Object[] { })).booleanValue()) {
                    // sProxyHost = proxyInfo.getProxy();
                    sProxyHost = (String)ProxyInfo.getMethod("getSocksProxy", new Class[]
{ }).invoke(proxyInfo, new Object[] { });
                    // nProxyPort = proxyInfo.getPort();
                    nProxyPort = ((Integer)ProxyInfo.getMethod("getSocksPort", new Class[]
{ }).invoke(proxyInfo, new Object[] { })).intValue();
                    result = "SOCKS:" + sProxyHost + ":" + nProxyPort;
                }
                // if (proxyInfo.isProxyUsed()) {
                if (((Boolean)ProxyInfo.getMethod("isProxyUsed", new Class[] { })
.invoke(proxyInfo, new Object[] { })).booleanValue()) {
                    // sProxyHost = proxyInfo.getProxy();
                    sProxyHost = (String)ProxyInfo.getMethod("getProxy", new Class[] { })
.invoke(proxyInfo, new Object[] { });
                    // nProxyPort = proxyInfo.getPort();
                    nProxyPort = ((Integer)ProxyInfo.getMethod("getPort", new Class[] { })
.invoke(proxyInfo, new Object[] { })).intValue();
                    result = "" + sProxyHost + ":" + nProxyPort;
                }
            } catch (Throwable e) {
               if (outP != null) {
                   outP.put("problemsInJdk13ProxySetting",new Boolean(true));
               }
            }
        }
        else if(sJavaVer.startsWith("1.4")) {// if plugin 1.4
            try {
                String     sProxyHost    = null;
                int     nProxyPort    = -1;

                // java.net.URL url = new java.net.URL(inurl);
                // proxyInfo =  sun.plugin.net.proxy.PluginProxyManager.getProxyInfo
(url);
                Class     PluginProxyHandler    = Class.forName
("sun.plugin.net.proxy.PluginProxyManager");
                Method     getDefaultProxyHandler    = PluginProxyHandler.getMethod
("getProxyInfo",new Class[] { java.net.URL.class });
                Object     proxyInfo    = getDefaultProxyHandler.invoke(null, new Object[] {
new java.net.URL(inurl) });

                // need this for method invocation!!!!
                Class     ProxyInfo    = Class.forName("sun.plugin.net.proxy.ProxyInfo");

                // if (proxyInfo != null) {
                if (proxyInfo != null) {
                    // sProxyHost = proxyInfo.getProxy();
                    sProxyHost = (String)ProxyInfo.getMethod("getProxy", new Class[] { })
.invoke(proxyInfo, new Object[] { });
                    // nProxyPort = proxyInfo.getPort();
                    nProxyPort = ((Integer)ProxyInfo.getMethod("getPort", new Class[] { })
.invoke(proxyInfo, new Object[] { })).intValue();
                    result = "" + sProxyHost + ":" + nProxyPort;
                }
            } catch (Throwable e) {
            }
        }
        else if(sJavaVer.startsWith("1.5")) {// if plugin 1.5
           try {
               Class proxySelectorClass = Class.forName("java.net.ProxySelector")
;
               Method proxySelector_getDefault = proxySelectorClass.getMethod
("getDefault",new Class[] {});
               Object ps = proxySelector_getDefault.invoke(null,new Object[] {});
               java.net.URI uri = null;
               try {
                   uri = new java.net.URI(inurl);
               }
               catch (Exception e) {
                   // Handle exception
               }

               Method proxySelector_select = proxySelectorClass.getMethod
("select",new Class[] { uri.getClass() });
               Object proxyListObject = proxySelector_select.invoke(ps,new
Object[] { uri });

               if (proxyListObject instanceof java.util.List) {
                   List proxyList = (List) proxyListObject;
                   int len = proxyList.size();
                   for (int i = 0; i < len; i++) {
                       Object p = proxyList.get(i);

                       Class proxyClass = Class.forName("java.net.Proxy");
                       Method proxyClass_address = proxyClass.getMethod
("address",new Class[] {});
                       Object addrObject = proxyClass_address.invoke(p,new
Object[] { });
                       InetSocketAddress addr = (InetSocketAddress) addrObject;

                       if (addr == null) {
                           // Use a direct connection - no proxy
                       }
                       else {
                           InetAddress ip_addr = addr.getAddress();
                           String     sProxyHost    = ip_addr.toString();
                           int     nProxyPort    = addr.getPort();

                           result = "" + sProxyHost + ":" + nProxyPort;
                           // Use the specified IP and port number as the proxy
                       }
                   }
               }
           } catch (Throwable e) {

           }
        }

        return result;
    }

looking forward to any comments
Gulpi


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.