自己手動寫http伺服器(2)

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   ar   os   sp   div   

 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

 

控制台的結果是

GET / HTTP/1.1Host: localhost:8088Connection: keep-aliveAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36Accept-Encoding: gzip,deflate,sdchAccept-Language: zh-CN,zh;q=0.8HTTP/1.1 200 OKServer: tomcatDateSun Nov 09 20:42:08 CST 2014Content-type:text/html;charset=GBKContent-Length:89<html><head><titilt>你怎麼捨得我難過</title>大姑娘美,大姑娘浪</head><body></body></html>

  

 

自己手動寫http伺服器(2)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.