Note: Read the Code example disclaimer for important legal information.
////////////////////////////////////////////////////////////////////////////////// // // This example client program utilizes an SSLContext object, which it initializes // to use the "MY_CLIENT_APP" application ID. // // The example uses the native iSeries JSSE provider, regardless of the // properties specified by the java.security file. // // Command syntax: // java -Djava.version=1.4 SslClient // // Note that "-Djava.version=1.4" is unnecessary when you have configured // J2SDK version 1. to be used by default. // ////////////////////////////////////////////////////////////////////////////////// import java.io.*; import javax.net.ssl.*; /** * SSL Client Program. */ public class SslClient { /** * SslClient main method. * * @param args the command line arguments (not used) */ public static void main(String args[]) { /* * Set up to catch any exceptions thrown. */ try { /* * Initialize an SSLConfiguration object to specify an application * ID. "MY_CLIENT_APP" must be registered and configured * correctly with the Digital Certificate Manager (DCM). */ SSLConfiguration config = new SSLConfiguration(); config.setApplicationId("MY_CLIENT_APP" /* * Get a KeyStore object from the SSLConfiguration object. */ Char[] password = "password".toCharArray(); KeyStore ks = config.getKeyStore(password); /* * Allocate and initialize a KeyManagerFactory. */ KeyManagerFactory kmf = KeyManagerFactory.getInstance("IbmISeriesX509"); Kmf.init(ks, password); /* * Allocate and initialize a TrustManagerFactory. */ TrustManagerFactory tmf = TrustManagerFactory.getInstance("IbmISeriesX509"); tmf.init(ks); /* * Allocate and initialize an SSLContext. */ SSLContext c = SSLContext.getInstance("SSL", "quot;); C.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); /* * Get the an SSLSocketFactory from the SSLContext. */ SSLSocketFactory sf = c.getSocketFactory(); /* * Create an SSLSocket. * * Change the hard-coded IP address to the IP address or host name * of the server. */ SSLSocket s = (SSLSocket) sf.createSocket("1.1.1.1", 13333); /* * Send a message to the server using the secure session. */ String sent = "Test of java SSL write"; OutputStream os = s.getOutputStream(); os.write(sent.getBytes()); /* * Write results to screen. */ System.out.println("Wrote " + sent.length() + " bytes..."); System.out.println(sent); /* * Receive a message from the server using the secure session. */ InputStream is = s.getInputStream(); byte[] buffer = new byte[1024]; int bytesRead = is.read(buffer); if (bytesRead == -1) throw new IOException("Unexpected End-of-file Received"); String received = new String(buffer, 0, bytesRead); /* * Write results to screen. */ System.out.println("Read " + received.length() + " bytes..."); System.out.println(received); } catch (Exception e) { System.out.println("Unexpected exception caught: " + e.getMessage()); e.printStackTrace(); } } }