| |
36. 45. 3. 使用JSSEHTTPS服务器 |
|
import java.io.*;
import java.net.*;
import javax.net.ssl.*;
public class MainClass {
public static void main(String[] args) throws IOException {
SSLServerSocketFactory ssf = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
SSLServerSocket ss = (SSLServerSocket) ssf.createServerSocket(8080);
ss.setNeedClientAuth(true);
while (true) {
try {
Socket s = ss.accept();
OutputStream out = s.getOutputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line = null;
while (((line = in.readLine()) != null) && (!("".equals(line)))) {
System.out.println(line);
}
System.out.println("");
StringBuffer buffer = new StringBuffer();
buffer.append("<HTML>\n");
buffer.append("<HEAD><TITLE>HTTPS Server</TITLE></HEAD>\n");
buffer.append("<BODY>\n");
buffer.append("<H1>Success!</H1>\n");
buffer.append("</BODY>\n");
buffer.append("</HTML>\n");
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();
out.close();
in.close();
s.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
|
|
|