1 17 18 package tutorial; 19 20 import java.io.DataOutputStream ; 21 import java.io.IOException ; 22 import java.io.OutputStream ; 23 import java.util.Date ; 24 25 30 public class Reply { 31 34 public final static String CRLF = "\r\n"; 35 38 public final static String HTTP_VERSION = "HTTP/1.0"; 39 42 public final static String WEB_SERVER_NAME = "Merlin Simple Server/1.0"; 43 46 public final static String HTTP_200 = HTTP_VERSION + " 200 OK" + CRLF; 47 50 public final static String HTTP_400 = HTTP_VERSION + " 400 Bad Request" + 51 CRLF; 52 55 public final static String HTTP_403 = HTTP_VERSION + " 404 Forbidden" + CRLF; 56 59 public final static String HTTP_404 = HTTP_VERSION + " 404 Not Found" + CRLF; 60 63 public final static String HTTP_405 = HTTP_VERSION + 64 " 405 Method Not Allowed" + CRLF; 65 66 69 private Reply() { 70 } 71 72 79 public static void sendHttpReply(OutputStream os, String type, String msg) throws 80 IOException { 81 82 DataOutputStream dos = new DataOutputStream (os); 83 dos.writeBytes(type); 84 dos.writeBytes("Connection: close" + CRLF); 85 dos.writeBytes("Server: " + WEB_SERVER_NAME + CRLF); 86 dos.writeBytes("Date: " + RfcDateFormat.format(new Date ()) + CRLF); 87 dos.writeBytes("Content-Type: text/html; charset=iso-8859-1" + CRLF); 88 dos.writeBytes("Content-Length: " + msg.length() + CRLF); 90 if (type.equals(HTTP_405)) { 91 dos.writeBytes("Allow: GET" + CRLF); 92 } 93 dos.writeBytes(CRLF); 94 95 String s = new String ("<html><body><h1>" + msg + "</h1></body></html>"); 96 dos.writeUTF(s); 98 dos.flush(); 99 dos.close(); 100 } 101 } 102 | Popular Tags |