手写服务器httpserver_准备_http协议_http工具JAVA197-198
2016-03-17 21:09
585 查看
来源:http://www.bjsxt.com/
一、S02E197_01手写服务器httpserver_准备_http协议
二、S02E198_01手写服务器httpserver_准备_http工具
HTTP响应分析
HTTP工具:HttpWatch——捕捉信息,便于查看请求响应内容
一、S02E197_01手写服务器httpserver_准备_http协议
package com.test.server; import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStreamWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.Date; /** * 创建服务器,并启动 * 1、请求 * 2、响应 */ public class ServerForResponse { private ServerSocket server; private static final String CRLF = "\r\n"; private static final String BLANK = " "; public static void main(String[] args) { ServerForResponse server = new ServerForResponse(); server.start(); } /** * 启动方法 */ public void start(){ try { server = new ServerSocket(8888); this.receive(); } catch (IOException e) { e.printStackTrace(); } } /** * 接收客户端 */ private void receive(){ try { Socket client =server.accept(); StringBuilder sb =new StringBuilder(); byte[] date = new byte[20480]; int len = client.getInputStream().read(date); //接收客户端的请求信息 String requestInfo = new String(date,0,len).trim(); System.out.println(requestInfo); //响应 StringBuilder responseContext = new StringBuilder(); responseContext.append("<html><head><title>HTTP响应示例</title>" + "</head><body>Hello World!</body></html>"); StringBuilder response = new StringBuilder(); //1)HTTP协议版本、状态代码、描述 response.append("HTTP/1.1").append(BLANK).append("200").append(BLANK).append("OK").append(CRLF); //2)响应头(Response Head) response.append("Server:test Server/0.0.1").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(responseContext.toString().getBytes().length).append(CRLF); //3)正文之前 response.append(CRLF); //4)正文 response.append(responseContext); //输出流 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(client.getOutputStream())); bw.write(response.toString()); bw.flush(); bw.close(); } catch (IOException e) { //e.printStackTrace(); } } /** * 停止服务器 */ public void stop(){ } }
二、S02E198_01手写服务器httpserver_准备_http工具
HTTP响应分析
HTTP工具:HttpWatch——捕捉信息,便于查看请求响应内容
相关文章推荐
- Http的会话跟踪和跨站攻击(xss)
- Http的会话跟踪和跨站攻击(xss)
- Http的会话跟踪和跨站攻击(xss)
- Http的定义及其基本概念介绍
- Http的定义及其基本概念介绍
- Http的定义及其基本概念介绍
- git 如何删除本地创建的仓库(转载自 https://segmentfault.com/q/1010000002996177?_ea=262685)
- 【web必知必会】——图解HTTP
- 机器视觉系列—— Vision 基础知识上集 http://bbs.elecfans.com/jishu_466819_1_1.html (出处: 中国电子技术论坛)
- 网络基础
- Eclipse-new-Class文件时Interfaces-Add-Choose interfaces里找不到GenericServlet和HttpServlet
- Python TCP编程入门学习:(1)
- TCP/IP、Http的区别
- window下配置SSH连接GitHub、GitHub配置ssh key(转载自 http://jingyan.baidu.com/article/a65957f4e91ccf24e77f9b11.html)
- Java 网络编程(二)
- python https 加密传输
- IOS 网络浅析 (二 网络异步请求)
- ubuntu系统 有线网络无法通过网页认证上网
- 43.https自建证书在 AFN中的设置
- Android架构:用消息机制获取网络数据