Proxy Authentication for Java

If you access the Internet from behind a firewall, the connection�s Proxy Authorization property has to be set. The ways to set the proxy property differ between HTTP protocol and HTTPS protocol. Since HTTP protocol is not utilized with UPS OnLine Tools, the HTTPS protocol implementation will be detailed below using both SSLite and JSSE.

 

HTTPS Protocol

Code Examples

The following code example illustrates an implementation using SSLite:

 

System.getProperties().put("https.proxyHost", proxy);

 

System.getProperties().put("https.proxyPort", port);

 

// do this anytime system properties have been related to HTTPS handler

com.ibm.samplepackage.https.HttpsClient.resetProperties();

 

// Setup proxy authentication com.ibm.samplepackage.https.HttpsURLConnection.setDefaultAuthenticator(new Proxy(username, password));

 

 

Class Proxy implements the interface of com.ibm.samplepackage.https.HttpsAuthenticator. The source code of Proxy is here.

 

The following code example illustrates an implementation using JSSE:

 

System.getProperties().put("https.proxyHost", proxy);

 

System.getProperties().put("https.proxyPort", port);

 

// Setup proxy authentication

 

java.net.Authenticator.setDefault (new PasswordAuthenticator());

 

PasswordAuthenticator class is defined as the following:

 

import java.net.Authenticator;

import java.net.PasswordAuthentication;

 

class PasswordAuthenticator extends Authenticator {

 

 protected PasswordAuthentication getPasswordAuthentication() {

System.out.println("getPasswordAuthentication() called for https connection!!!");

        return new PasswordAuthentication(strUser, "password".toCharArray());

    }

}

 

 

JDK version 1.2.1 or greater is required for the class java.net.Authenticator.

 

Here is the completed source code of the class XmlTransmitter, which handles reading the XML request file, sending the request to UPS Online Tool server, and writing the XML response to a file. This class uses SSLite for Java 1.1.x . In order for the sample code to function, the sslite.jar file must be added to the client machine's class path. Included with the sslite.jar is the file named KeyRing.pfx that must be used for a successful SSLite implementation. It is recommended that you follow the examples provided by your particular SSL package when implementing the respective code.

 

If you already have an XML request file and want to have the XML response write to a file, you can run XmlTransmitter <service><configuration filename><xml request filename><xml response filename>. The configuration file sets the server's host name, connection protocol, location of the file KeyRing.pfx, and proxy server properties. A sample configuration file is here.  You can modify the values based on your application.

 

Please refer to "Working with Java Sample Code" before executing or compiling and source code found within this documentation.

 

 

Return to Top

 

Copyright � 2006 United Parcel Service of America, Inc.