httpServer:package com.qiyang.httpServer;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class HttpServer {
private static ServerSocket ss;
public static void main(String[] args) throws IOException {
ss = new ServerSocket(8888);
while (true) {
Socket socket = ss.accept();
InputStreamReader ISR = new InputStreamReader(socket.getInputStream());
// 获取客户端输入流 请求头,换行符,请求体
BufferedReader bd = new BufferedReader(ISR);
String requestHeader;
while ((requestHeader = bd.readLine()) != null && !requestHeader.isEmpty()) {
System.out.println(requestHeader);
}
// 发送回执
PrintWriter pw = new PrintWriter(socket.getOutputStream());
pw.println("HTTP/1.1 200 OK");
pw.println("Content-type:text/html");
pw.println();
pw.println("
Hello World! This is test page
");pw.flush();
socket.close();
}
}
}