<强>反应:强>
<代码>{公共类反应 私人BufferedWriter bw;//正文 私人StringBuilder内容;//协议头(状态行与请求头回车)信息 私人StringBuilder headInfo; 私人int len;//正文的字节数 私人最终字符串空白=" "; 私人最终字符串CRLF=" \ r \ n "; 私人的反应(){ 内容=new StringBuilder (); headInfo=new StringBuilder (); len=0; } 公开回应(插座客户机){ 这(); 尝试{ bw=new BufferedWriter(新OutputStreamWriter (client.getOutputStream ())); }捕捉(IOException e) { e.printStackTrace (); headInfo=零; } } 公开回应(OutputStream os) { 这(); bw=new BufferedWriter(新OutputStreamWriter (os)); }//动态添加内容 公开回应打印(字符串信息){ content.append(信息); len +=info.getBytes . length (); 返回; } 公开回应println(字符串信息){ content.append(信息).append (CRLF); len +=(信息+ CRLF) .getBytes . length (); 返回; }//推送响应信息 公共空间pushToBrowser (int代码)抛出IOException { 如果(null==headInfo) { 代码=505; } createHeadInfo(代码); bw.append (headInfo); bw.append(内容); bw.flush (); }//构建头信息 私人空间createHeadInfo (int代码){//1,响应行:HTTP/1.1 200 OK headInfo.append (“HTTP/1.1”) .append(空白); headInfo.append(代码).append(空白); 开关(代码){ 200年情况: headInfo.append (“OK”) .append (CRLF); 打破; 404年情况: headInfo。追加(“没有找到”).append (CRLF); 打破; 505年情况: headInfo。追加(“服务器错误”).append (CRLF); 打破; }//2、响应头(最后一行存在空行): headInfo.append(“日期:”)。append())(新日期.append (CRLF); headInfo.append(“服务器:”)。追加(“shsxt服务器/0.0.1;charset=GBK”) .append (CRLF); headInfo.append (content - type: text/html) .append (CRLF); 内容长度headInfo.append (“:”) .append (len) .append (CRLF); headInfo.append (CRLF); } }代码>
<强>要求:强>
{<代码>公共类请求//协议信息 私人字符串requestInfo;//请求方式 私人字符串方法;//请求url 私人字符串url;//请求参数 私人字符串queryStr; 私人最终字符串CRLF=" \ r \ n "; 公开请求(插座端)抛出IOException { 这(client.getInputStream ()); } 公共请求InputStream) { byte[]数据=新字节(1024 * 1024); int len; 尝试{ len=is.read(数据); 这一点。requestInfo=新的字符串(数据0 len); }捕捉(IOException e) { e.printStackTrace (); 返回; }//分解字符串 parseRequestInfo (); } 私人空间parseRequestInfo () { System.out.println(“- - - - - -分解- - - - - - -”); system . out。println(“, 1获取请求方式:开头到第一个/- - - - - -”); 这一点。=this.requestInfo方法。substring (0, this.requestInfo.indexOf (“/?) .toLowerCase (); this.method=this.method.trim (); system . out。println(“, 2,获取请求url:第一个/到HTTP/- - - - - -”); System.out.println(“- - - - - -可能包含请求参数?前面的url为- - - - - -”);//1),获?的位置 int startIdx=this.requestInfo.indexOf (“/? + 1;//2),获取HTTP/的位置 int endIdx=this.requestInfo.indexOf (“HTTP/?;//3),分割字符串 这一点。url=this.requestInfo。substring (startIdx endIdx);//4),获取?的位置 int queryIdx=this.url.indexOf (“?”); 如果(queryIdx>=0){//表示存在请求参数,考虑在第一个位置的情况 String [] urlArray=this.url.split (“\ \ ?”); 这一点。url=urlArray [0]; queryStr=urlArray [1]; } System.out.println (this.url); System.out.println(“3,获取请求参数:如果得到已经获取,如果是文章可能在请求体中- - - - - -”); 如果(method.equals(“文章”)){ 字符串qStr=this.requestInfo.substring (this.requestInfo.lastIndexOf (CRLF) .trim (); System.out.println (qStr +”——在“); 如果(null==queryStr) { queryStr=qStr; 其他}{ queryStr +="和" + qStr; } } queryStr=null==queryStr ?”“: queryStr; System.out.println url(方法+”——在“+ +”——在“+ queryStr); } }代码>服务器:
<代码>公共类Server04 { 私人ServerSocket考察一下; 公共静态void main (String [] args) { Server04服务器=new Server04 (); server.start (); }//启动服务 公共空间开始(){ 尝试{ serverSocket=考察(8888); 收到(); }捕捉(IOException e) { e.printStackTrace (); System.out.println(“服务器启动失败....”); } }//接受连接处理 公共空间接收(){ 尝试{ 套接字客户=serverSocket.accept (); System.out.println(“一个客户端建立了连接....”);//获取请求协议 请求请求=新请求(客户端);//关注了内容 响应响应=new响应(客户端);//创建好了输出流 response.print (“& lt; html>”);//通过输出流输出 response.print (“& lt; head>”); response.print (“& lt; title>”); response.print(“服务器响应成功”); response.print (“& lt;/title>”); response.print (“& lt;/head>”); response.print (“& lt; body>”); 响应。print (“shsxt服务器终于回来了....”); response.print (“& lt;/body>”); response.print (“& lt;/html>”);//关注了状态码 response.pushToBrowser (200); }捕捉(IOException e) { e.printStackTrace (); System.out.println(“客户端错误”); } }//停止服务 公共空间停止(){ } }java网络服务器——封装请求协议2