import java.io.*; import java.net.*; import javax.net.ssl.*; public class ServidorHTTPS { public static void main(String[] args) throws IOException { // Utilizar una SocketFactory para crear sockets SSL: SSLServerSocketFactory ssf = (SSLServerSocketFactory)SSLServerSocketFactory.getDefault(); ServerSocket ss = ssf.createServerSocket(8080); // Bucle infinito para aceptar conexiones permanentemente while (true) { try { Socket s = ss.accept(); // Los streams de entrada y salida están encriptados // pero esto es transparente OutputStream out = s.getOutputStream(); BufferedReader in = new BufferedReader( new InputStreamReader(s.getInputStream())); // Leer la entrada del cliente y mostrarla en la pantalla String linea = null; while (((linea = in.readLine())!= null) && (!("".equals(linea)))) { System.out.println(linea); } System.out.println(""); // Construir una respuesta StringBuffer buffer = new StringBuffer(); buffer.append("\n"); buffer.append( "HTTPS Server\n"); buffer.append("\n"); buffer.append("

Hola!

\n"); buffer.append("\n"); buffer.append("\n"); // HTTP requiere un content-length. String string = buffer.toString(); byte[] data = string.getBytes(); out.write("HTTP/1.0 200 OK\n".getBytes()); out.write(new String( "Content-Length: "+data.length+"\n").getBytes()); out.write("Content-Type: text/html\n\n".getBytes()); out.write(data); out.flush(); // Cerrar los streams y el socket. out.close(); in.close(); s.close(); } catch (Exception e) { e.printStackTrace(); } } // del while } // del main() }