152 lines
6.2 KiB
HTML
152 lines
6.2 KiB
HTML
|
<?xml version="1.0" encoding="UTF-8"?>
|
||
|
<!DOCTYPE html
|
||
|
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||
|
<html lang="en-us" xml:lang="en-us">
|
||
|
<head>
|
||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||
|
<meta name="security" content="public" />
|
||
|
<meta name="Robots" content="index,follow" />
|
||
|
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
|
||
|
<meta name="DC.Type" content="reference" />
|
||
|
<meta name="DC.Title" content="Example: SSL client using an SSLContext object" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="codedisclaimer.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="rzahajsseexmpls.htm" />
|
||
|
<meta name="copyright" content="(C) Copyright IBM Corporation 2006" />
|
||
|
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 2006" />
|
||
|
<meta name="DC.Format" content="XHTML" />
|
||
|
<meta name="DC.Identifier" content="rzahajsseexmplclient" />
|
||
|
<meta name="DC.Language" content="en-us" />
|
||
|
<!-- All rights reserved. Licensed Materials Property of IBM -->
|
||
|
<!-- US Government Users Restricted Rights -->
|
||
|
<!-- Use, duplication or disclosure restricted by -->
|
||
|
<!-- GSA ADP Schedule Contract with IBM Corp. -->
|
||
|
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
|
||
|
<link rel="stylesheet" type="text/css" href="./ic.css" />
|
||
|
<title>Example: SSL client using an SSLContext object</title>
|
||
|
</head>
|
||
|
<body id="rzahajsseexmplclient"><a name="rzahajsseexmplclient"><!-- --></a>
|
||
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
||
|
<h1 class="topictitle1">Example: SSL client using an SSLContext object</h1>
|
||
|
<div><div class="section"><p><strong>Note:</strong> Read the <a href="codedisclaimer.htm">Code
|
||
|
example disclaimer</a> for important legal information.</p>
|
||
|
<pre>//////////////////////////////////////////////////////////////////////////////////
|
||
|
//
|
||
|
// 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();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}</pre>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div>
|
||
|
<div class="familylinks">
|
||
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="rzahajsseexmpls.htm" title="The JSSE examples show how a client and a server can use the native iSeries JSSE provider to create a context that enables secure communications.">Examples: IBM Java Secure Sockets Extension</a></div>
|
||
|
</div>
|
||
|
|
||
|
<div class="linklist"><strong>Collected links</strong><br />
|
||
|
|
||
|
<div><a href="codedisclaimer.htm">Code example disclaimer</a></div></div>
|
||
|
</div>
|
||
|
</body>
|
||
|
</html>
|