Applets and Proxies


J2ME and the 6600.

JSP File upload progres bar.

Self signed applets

When you are writing applets, the occaision may arise where you need to know about the user's proxy configuration. In the normail run of things if you are using URLConnection or any of it's subclasses the java plug in takes care of proxy detection for you, however the plug in is very reluctant to share this information with you and as a result you need to look elsewhere for these settings.

Java 5 does have a and class however at the time of writing (Nov 2004) java 5 is nothing but a collection of bugs so we will not persue this any further. What we are going look at are the classes in package. This package consists of three classes:

  • ProxyInfo
  • ProxyService
  • ProxyServiceProvider

Out of these three the ProxyService class is the one that is going to provide us with the information that we seek:

   public void detect(String location)
      try {
	    ProxyInfo info[] = ProxyService.getProxyInfo(new URL(location));
	    if(info != null && info.length>0)
	      proxyHost = info[0].getHost();
		  proxyPort = info[0].getPort();
		  System.out.println("PROXY = " + proxyHost + ":" + proxyPort);
	  }catch (Exception ex) {
		   "could not retrieve proxy configuration, attempting direct connection.");
Sponsored Content

Rad Upload - A drag and drop file upload applet.

Thus if we call the getProxyInfo() method of the ProxyService class with the URL that we wish to connect to, it will return a ProxyInfo array which contains information about the proxy that our connection should be tunneled through. When we should attempt a direct connection this array will have a null value

Before you start jumping for joy, you should remember that the package we are using is undocumented. As such there is no guarantee that this bit of code will work on all platforms where a JVM could be used. In fact the code above fails on Mac OS X. So does that mean we are still stuck? not entirely.

When you embed your web page in an applet there is no reason for your to restrict yourself to static html. You could just as easily embed your applet in a PHP script or even html generated by a servlet. We could then inspect the HTTP headers to determine if the connection is through a proxy and pass that information to the applet.

The Via header which contains information about the proxies may look something like the following:

	1.1 (squid/2.5.STABLE6-20040820)
	1.1 localhost.localdomain:3128 (squid/2.5.STABLE1), 1.0 

In the second example above the first request had been made to a squid proxy running on localhost which had forwarded it to another runnong on We need to do a bit of string manipulation to extract this information and because PHP is so good at it, that's what we will use of the purpose of this demonstration. You could do the same just as easily with JSP or perl.

		$proxyHeaders = split(",",$_SERVER['HTTP_VIA']);
		$proxyHeaders = split(" ",$proxyHeaders[0]);
		echo "<aram name='proxyInfo' value='$proxyHeaders[1]'>"; 
		echo "<aram name='proxyInfo' value='none'>"; 


Notice how we pass this information to the applet as a parameter where it can be retrieved by a call to getParameter('proxyInfo');.

Finally before we wind up; let's note that it is possible but unlikely that some proxies may choose not to send the Via header. It is also possible that different are tunnelled through different proxies and as a result the information that you retrieve from the headers may be invalid. So the bottom line is that until java 5.0 becomes stable enough to use we are stuck. Therefor it is recommended that you combine both approaches instead of using one or the other.