码迷,mamicode.com
首页 > Web开发 > 详细

自己手动写http服务器(2)

时间:2014-11-09 22:01:51      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   ar   os   sp   div   

bubuko.com,布布扣
 1 tringBuilder response =new StringBuilder();
 2             //1)  HTTP协议版本、状态代码、描述
 3             response.append("HTTP/1.1").append(BLANK).append("200").append(BLANK).append("OK").append(CRLF);
 4             //2)  响应头(Response Head)
 5             response.append("Server:bjsxt Server/0.0.1").append(CRLF);
 6             response.append("Date:").append(new Date()).append(CRLF);
 7             response.append("Content-type:text/html;charset=GBK").append(CRLF);
 8             //正文长度 :字节长度
 9             response.append("Content-Length:").append(responseContext.toString().getBytes().length).append(CRLF);
10             //3)正文之前
11             response.append(CRLF);
12             //4)正文
13             response.append(responseContext);
14             
15             System.out.println(responseContext);
16             
View Code

先上代码,和结果。执行程序,浏览器中输入localhost:8088

bubuko.com,布布扣

 

控制台的结果是

GET / HTTP/1.1
Host: localhost:8088
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: zh-CN,zh;q=0.8


HTTP/1.1 200 OK
Server: tomcat
DateSun Nov 09 20:42:08 CST 2014
Content-type:text/html;charset=GBK
Content-Length:89

<html><head><titilt>你怎么舍得我难过</title>大姑娘美,大姑娘浪</head><body></body></html>

  

 

自己手动写http服务器(2)

标签:style   blog   http   io   color   ar   os   sp   div   

原文地址:http://www.cnblogs.com/hansongjiang/p/4085859.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!