Some reader favorites:
EJB fundamentals and session beans
Create a scrollable virtual desktop in Swing
Wizard API updated!
Tim Boudreau has released a new version of the Swing Wizard library (version 0.997) that fixes the WizardException bug reported in JavaWorld's recent Open Source Java Project profile. The article's examples have been reworked to test out the new, improved WizardException. Thanks, Tim, for this helpful fix!
Open Source Java Projects: The Wizard API
The secret to combining Java and proxies lies in activating certain system properties in the Java runtime. These properties appear to be undocumented, and are whispered between programmers as part of the Java folklore. In order to work with a proxy, your Java application needs to specify information about the proxy itself as well as specify user information for authentication purposes. In your program, before you begin to work with any Internet protocols, you'll need to add the following lines:
System.getProperties().put( "proxySet", "true" ); System.getProperties().put( "proxyHost", "myProxyMachineName" ); System.getProperties().put( "proxyPort", "85" );
The first line above tells Java that you'll be using a proxy for your connections, the second line specifies the machine that the proxy lives on, and the third line indicates what port the proxy is listening on. Some proxies require a user to type in a username and password before Internet access is granted. You've probably encountered this behavior if you use a Web browser behind a firewall. Here's how to perform the authentication:
URLConnection connection = url.openConnection(); String password = "username:password"; String encodedPassword = base64Encode( password ); connection.setRequestProperty( "Proxy-Authorization", encodedPassword );
The idea behind the above code fragment is that you must adjust your HTTP header to send out your user information. This is
achieved with the setRequestProperty() call. This method allows you to manipulate the HTTP headers before the request is sent out. HTTP requires the user name and
password to be base64 encoded. Luckily, there are a couple of public domain APIs that will perform the encoding for you (see
the Resources section).
As you can see, there's not a whole lot to adding proxy support to your Java application. Given what you now know, and a little research (you'll have to find out how your proxy handles the protocol you're interested in and how to deal with user authentication), you can implement your proxy with other protocols.
Scott D. Taylor sent in the magic incantation to deal with proxying the FTP protocol:
defaultProperties.put( "ftpProxySet", "true" ); defaultProperties.put( "ftpProxyHost", "proxy-host-name" ); defaultProperties.put( "ftpProxyPort", "85" );
You can then access the files URLs using the "ftp" protocol via something like:
URL url = new URL("ftp://ftp.netscape.com/pub/navigator/3.04/windows/readme.txt" );
If anybody has examples of using a proxy with other Internet protocols, I'd love to see them.
Note: The example code (Example.java) has only been tested with JDK 1.1.4.
Free Download - 5 Minute Product Review. When slow equals Off: Manage the complexity of Web applications - Symphoniq
![]()
Free Download - 5 Minute Product Review. Realize the benefits of real user monitoring in less than an hour. - Symphoniq