java手动实现HTTP响应response
来源:互联网 发布:端口占用查询工具 编辑:程序博客网 时间:2024/09/21 09:03
private int len;
private static final String BLANK = " ";
private static final String CRLF = "\r\n";
//输出流
private BufferedWriter bw;
//头部
private StringBuilder headContent;
//正文
private StringBuilder content;
public Response() {
headContent = new StringBuilder();
content = new StringBuilder();
}
public Response(Socket socket) {
this();
try {
bw = new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//构建头部
public void head(int code) {
//1) HTTP协议版本、状态代码、描述
headContent.append("HTTP/1.1").append(BLANK);
switch(code) {
case 200 :
headContent.append("200").append(BLANK).append("OK");
break;
case 404:
headContent.append("404").append(BLANK).append("NOT Found");
break;
case 500:
headContent.append("500").append(BLANK).append("SERVER ERROR");
break;
}
headContent.append(CRLF);
//2) 响应头(Response Head)
headContent.append("Server:haojunjie Server/0.0.1").append(CRLF);
headContent.append("Date:").append(new Date()).append(CRLF);
headContent.append("Content-type:text/html;charset=GBK").append(CRLF);
//正文长度 :字节长度
headContent.append("Content-Length:").append(len).append(CRLF);
//3)正文之前
headContent.append(CRLF);
len += headContent.length();
}
//构建正文
public void content(String str) {
content.append(str);
len += str.length();
}
public void pushToClient(int code) {
head(code);
try {
bw.append(headContent);
bw.append(content);
bw.flush();
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class Server4 {
private ServerSocket server;
public static final String CRLF="\r\n";
public static final String BLANK=" ";
/**
* @param args
*/
public static void main(String[] args) {
Server4 server = new Server4();
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();
byte[] data=new byte[20480];
int len =client.getInputStream().read(data);
//接收客户端的请求信息
String requestInfo=new String(data,0,len).trim();
System.out.println(requestInfo);
//响应
Response rep=new Response(client);
rep.content("<html><head><title>HTTP响应示例</title>");
rep.content("</head><body>我是最强王者</body></html>");
rep.pushToClient(200);
} catch (IOException e) {
}
}
/**
* 听着服务器
*/
public void stop(){
}
}
- java手动实现HTTP响应response
- java 实现http响应
- HTTP 响应Response
- HTTP之响应消息Response
- 怎样接收http响应(http response)
- 使用JAVA发送HTTP请求(Http Request),返回HTTP响应(Http Response)内容,代码程序例子及原理说明
- java实现简单的HTTP响应
- http协议之response响应详解
- http协议学习-响应头Response Headers
- java 发送 json、xml格式的 http请求,并读取响应response内容实例
- HTTP Response Status Code -- HTTP响应代码中文详解
- HTTP Response Status Code -- HTTP响应代码中文详解
- HTTP Response Status Code -- HTTP响应代码中文详解
- HTTP响应代码中文详解(HTTP Response Status Code)
- HTTP Response Status Code -- HTTP响应代码中文详解
- HTTP Response Status Code -- HTTP响应代码中文详解
- HTTP Response Status Code -- HTTP响应代码中文详解
- HTTP Response Status Code -- HTTP响应代码中文详解
- 简单的Fresco加载图片
- JAVA中IO流
- 早安1128
- Windows 下 zookeeper 部署
- Grafana+Prometheus系统监控之Redis
- java手动实现HTTP响应response
- Uncaught ReferenceError: $ is not defined? 的解决方法
- 依赖倒转原则
- ThinkPHP5框架集成第三方登陆SDK实现登陆功能
- 通过SyncToy进行服务器定时服务备份流程
- 《ARM处理器体系架构与软件编程》12.15-16上海
- Linux perf: 为什么采样频率设置为99Hz而不是100Hz?
- 《Linux系统编程与调试调优》四天线下课程--2018年1月17-20日上海
- (三)Spring Cloud架构的代码结构