標籤:style blog http io color ar os sp java
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="\r\n";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 blockhead=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.12Date:Mon,6Oct2003 13:23:42 GMTContent-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="\r\n"; 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
會返回我們的網頁,你可以試試
自己手寫http伺服器 http響應資訊的封裝與測試