Home | Mirror | Search

2. Java SSL Socket Client

		
package netkiller;

import java.io.*;
import java.net.*;
import javax.net.SocketFactory;
import javax.net.ssl.*;

public class SSLClientSocket {

	public static void main(String[] args) {
	    try {
	        int port = 443;
	        String hostname = "java.sun.com";

	        SocketFactory socketFactory = SSLSocketFactory.getDefault();
	        Socket socket = socketFactory.createSocket(hostname, port);

	        // Create streams to securely send and receive data to the server
	        InputStream in = socket.getInputStream();
	        OutputStream out = socket.getOutputStream();

	        BufferedReader socketReader = new BufferedReader(new InputStreamReader(in));
	        PrintWriter socketWriter = new PrintWriter(out);

	        socketWriter.println("GET /");
	        socketWriter.flush();
	        String line=null;
	        StringBuffer html = new StringBuffer();
	        while((line=socketReader.readLine())!=null){
	        	html.append(line+"\n");
	        }
	        // Read from in and write to out...
	        System.out.println(html.toString());

	        // Close the socket
	        socketReader.close();
	        socketWriter.close();
	        in.close();
	        out.close();
	    } catch(IOException e) {
	    }

	}

}

		
		
comments powered by Disqus