//assumption - certificate already loaded in cacerts file
//@see also java web service security post
package com.yourhost.socket.client;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.security.KeyStore;
import javax.net.SocketFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;
public class GB_GRIPSMXWS_SocketClient {
public static final String SOCKET_SERVER_HOST = "www.yourhost.com";
public static int SOCKET_SERVER_PORT = 443;
int timeout = 10000;
/**
* @param args
*/
public static void main(String[] args) {
try {
int port = SOCKET_SERVER_HOST;
String hostname = SOCKET_SERVER_PORT;
char[] passphrase = "changeit".toCharArray();
File file = new File("jssecacerts");
if (file.isFile() == false) {
char SEP = File.separatorChar;
File dir = new File(System.getProperty("java.home") + SEP
+ "lib" + SEP + "security");
file = new File(dir, "jssecacerts");
if (file.isFile() == false) {
file = new File(dir, "cacerts");
}
}
System.out.println("Loading KeyStore " + file + "...");
InputStream in = new FileInputStream(file);
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(in, passphrase);
in.close();
SSLContext context = SSLContext.getInstance("TLS");
TrustManagerFactory tmf =
TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(ks);
X509TrustManager defaultTrustManager = (X509TrustManager)tmf.getTrustManagers()[0];
context.init(null, new TrustManager[] {defaultTrustManager}, null);
SSLSocketFactory factory = context.getSocketFactory();
System.out.println("Opening connection to " + hostname + ":" + port + "...");
SSLSocket socket = (SSLSocket)factory.createSocket(hostname, port);
socket.setSoTimeout(timeout);
try {
System.out.println("Starting SSL handshake...");
socket.startHandshake();
System.out.println();
System.out.println("No errors, certificate is already trusted");
BufferedWriter writer =
new BufferedWriter(new OutputStreamWriter(
socket.getOutputStream()));
BufferedReader reader =
new BufferedReader(new InputStreamReader(
socket.getInputStream()));
// write reply to the output
//hidden -> serialize XML message to the writer
writer.write("
writer.write("
writer.write("
writer.write("
writer.write("
writer.write("
writer.write("
writer.write("
writer.write("
writer.write("
writer.write("
writer.write("
writer.write("
//so on
writer.write("
writer.write("
writer.flush();
String messageLine = null;
while ((messageLine=reader.readLine())!= null) {
System.out.println(messageLine);
}
reader.close();
writer.close();
socket.close();
} catch (SSLException ex) {
System.out.println();
ex.printStackTrace(System.out);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
No comments:
Post a Comment