java webserver-封装request请求协议
封装请求协议:获取method uri以及请求参数
public class Request { //协议信息 private String requestInfo; //请求方式 private String method; //请求的uri private String url; //请求的参数 private String queryStr; public Request(InputStream is) { byte[] datas=new byte[1024*1024]; int len = 0; try { len=is.read(datas); this.requestInfo =new String(datas,0,len); System.out.println(requestInfo); } catch (IOException e) { e.printStackTrace(); return; } //分解字符串 parseRequestInfo();}public Request(Socket client) throws IOException{ this(client.getInputStream()); //构造器的相互调用,只能放在首行}private void parseRequestInfo(){ System.out.println("----分解----"); //获取请求方式 System.out.println("1、获取请求方式:开头到第一个/之间"); this.method=this.requestInfo.substring(0,this.requestInfo.indexOf("/")).toLowerCase(); //获取url System.out.println("2、获取url:第一个/到HTTP/第一个字母之间"); System.out.println("---可能包含请求参数?前面的为url"); //url:获取第一个/的位置 int startIdx1=this.requestInfo.indexOf("/")+1; //要/之后的内容 //url:获取HTTP/的第一个字母的位置 int endIdx2=this.requestInfo.indexOf("HTTP/"); //返回第一个字母的位置 //url:分割字符串 this.url=this.requestInfo.substring(startIdx1,endIdx2); //获取?的位置 int queryIdx=this.url.indexOf("?"); //大于零表示存在 if(queryIdx>0) { String[] urlArray=this.url.split("\\?");//必须转义 this.url=urlArray[0]; queryStr=urlArray[1]; //请求参数 } System.out.println(this.url);}}
Server:
public class Server04 { private ServerSocket serverSocket ; public static void main(String[] args) { Server04 server = new Server04(); server.start(); } //启动服务 public void start() { try { serverSocket = new ServerSocket(8888); receive(); } catch (IOException e) { e.printStackTrace(); System.out.println("服务器启动失败...."); } } //接受连接处理 public void receive() { try { Socket client = serverSocket.accept(); System.out.println("一个客户端建立了连接...."); //获取请求协议 Request request=new Request(client); //关注了内容 Response response=new Response(client); //创建好了输出流 response.print("<html>"); //通过输出流输出 response.print("<head>"); response.print("<title>"); response.print("服务器响应成功"); response.print("</title>"); response.print("</head>"); response.print("<body>"); response.print("shsxt server终于回来了。。。。"); response.print("</body>"); response.print("</html>"); //关注了状态码 response.pushToBrowser(200); } catch (IOException e) { e.printStackTrace(); System.out.println("客户端错误"); }}//停止服务public void stop() {}}
Response:
public class Response { private BufferedWriter bw; //正文 private StringBuilder content; //协议头(状态行与请求头 回车)信息 private StringBuilder headInfo; private int len; //正文的字节数private final String BLANK =" ";private final String CRLF = "\r\n";private Response() { content =new StringBuilder(); headInfo=new StringBuilder(); len =0;}public Response(Socket client) { this(); try { bw=new BufferedWriter(new OutputStreamWriter(client.getOutputStream())); } catch (IOException e) { e.printStackTrace(); headInfo = null; }}public Response(OutputStream os) { this(); bw=new BufferedWriter(new OutputStreamWriter(os));}//流模式动态添加内容public Response print(String info) { content.append(info); len+=info.getBytes().length; return this;}public Response println(String info) { content.append(info).append(CRLF); len+=(info+CRLF).getBytes().length; return this;}//推送响应信息public void pushToBrowser(int code) throws IOException { if(null ==headInfo) { code = 505; } createHeadInfo(code); bw.append(headInfo); bw.append(content); bw.flush();}//构建头信息private void createHeadInfo(int code) { //1、响应行: HTTP/1.1 200 OK headInfo.append("HTTP/1.1").append(BLANK); headInfo.append(code).append(BLANK); switch(code) { case 200: headInfo.append("OK").append(CRLF); break; case 404: headInfo.append("NOT FOUND").append(CRLF); break; case 505: headInfo.append("SERVER ERROR").append(CRLF); break; } //2、响应头(最后一行存在空行): headInfo.append("Date:").append(new Date()).append(CRLF); headInfo.append("Server:").append("shsxt Server/0.0.1;charset=GBK").append(CRLF); headInfo.append("Content-type:text/html").append(CRLF); headInfo.append("Content-length:").append(len).append(CRLF); headInfo.append(CRLF); }}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。