自己手写http服务器 http响应信息的封装与测试



package cn.edu.sss.httpServer; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.Socket; import java.util.Date; //这个类是对http协议返回的封装 public class HttpResponse { public final String CRLF=" "; public final String BLANK=" "; //返回正文的长度 private int len; //返回状态行和请求头信息 private StringBuilder head; //返回正文内容 private StringBuilder content; //用于写到输出流中 private BufferedWriter bw; private HttpResponse() { len=0; content=new StringBuilder(); head=new StringBuilder(); } public HttpResponse(Socket s) { this(); try { bw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); } catch (IOException e) { // TODO Auto-generated catch block head=null; e.printStackTrace(); } } //构建正文 public void print(String s) { content.append(s); len=content.toString().getBytes().length; } public void println(String s) { content.append(s).append(CRLF); len=content.toString().length(); } /* private void createHeadInfo(int code){ //1) HTTP协议版本、状态代码、描述 headInfo.append("HTTP/1.1").append(BLANK).append(code).append(BLANK); switch(code){ case 200: headInfo.append("OK"); break; case 404: headInfo.append("NOT FOUND"); break; case 505: headInfo.append("SEVER ERROR"); break; } headInfo.append(CRLF); //2) 响应头(Response Head) headInfo.append("Server:bjsxt Server/0.0.1").append(CRLF); headInfo.append("Date:").append(new Date()).append(CRLF); headInfo.append("Content-type:text/html;charset=GBK").append(CRLF); //正文长度 :字节长度 headInfo.append("Content-Length:").append(len).append(CRLF); headInfo.append(CRLF); //分隔符 } //推送到客户端 */ private void createHeader(int code) { head.append("HTTP/1.1").append(BLANK).append(code).append(BLANK); switch(code) { case 200: head.append("OK");break; case 404: head.append("NOT FOUND"); } head.append(CRLF); head.append("Server:tomcat").append(CRLF); head.append("Date:").append(new Date()).append(CRLF); head.append("Content-type:text/html;charset=GBK").append(CRLF); head.append("Content-Length:").append(len).append(CRLF); head.append(CRLF); } public void flush(int code) throws IOException { createHeader(code); bw.write(head.toString()); bw.write(content.toString()); bw.flush(); } public static void main(String[] args) { // TODO Auto-generated method stub } }
HTTP/1.1 200 OK

 

Server:Apache Tomcat/5.0.12

Date:Mon,6Oct2003 13:23:42 GMT

Content-Length:112

好了,我们测试一下httpResponse的用法


package
cn.edu.sss.httpServer; import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStreamWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.Date; public class ServerDemo1 { public static final String BLANK=" "; public static final String CRLF=" "; public static void main(String args[]) throws IOException { ServerSocket server=new ServerSocket(8088); Socket socket=server.accept(); byte[] bytes=new byte[20000]; System.out.println(socket.getInetAddress()); int len=socket.getInputStream().read(bytes); String request=new String(bytes,0,len); System.out.println(request); HttpResponse response=new HttpResponse(socket); response.print("<html><head><titilt>你怎么舍得我难过</title>大姑娘美,大姑娘浪</head><body></body></html>"); response.flush(200);//指明响应码 /* //下面构造响应正文 StringBuilder sbu=new StringBuilder(); sbu.append("<html><head><titilt>你怎么舍得我难过</title>大姑娘美,大姑娘浪</head><body></body></html>"); // StringBuilder response=new StringBuilder(); response.append("HTTP/1.1").append(BLANK).append("200").append(BLANK).append("OK").append(CRLF); //响应头 response.append("Server: tomcat").append(CRLF); response.append("Date").append(new Date()).append(CRLF); response.append("Content-type:text/html;charset=GBK").append(CRLF); //正文长度,字节长度 response.append("Content-Length:").append(sbu.toString().getBytes().length).append(CRLF); response.append(CRLF); //加入正文 response.append(sbu); System.out.println(response); //返回给服务器端 BufferedWriter buf=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); buf.write(response.toString()); buf.flush();; buf.close(); */ } }


现在我们测试一下:在浏览器中输入网址

localhost:8088

会返回我们的网页,你可以试试

原文地址:https://www.cnblogs.com/hansongjiang/p/4094670.html