浏览器访问本地手写httpserver没有响应

浏览器访问本地手写httpserver没有响应

问题描述:

public class Server02 {
    private ServerSocket serverSocket;

    public static void main(String[] args) {
        Server02 server = new Server02();
        server.start();
    }
    //启动服务
    public void start() {
        try {
            serverSocket = new ServerSocket(8888);
            receive();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("服务器启动失败....");
        }
    }
    //
    public void receive() {
        try {
            Socket client = serverSocket.accept();
            System.out.println("一个客户端建立了连接....");

            InputStream is = client.getInputStream();
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
            //bw.write("hahahaha");
            byte[] datas = new byte[1024*1024];
            int len = is.read(datas);
            String requestInfo = new String(datas, 0, len);
            System.out.println(requestInfo);

            StringBuilder content = new StringBuilder();
            content.append("<html>");
            content.append("<head>");
            content.append("<title>");
            content.append("服务器响应成功");
            content.append("</title>");
            content.append("</head>");
            content.append("<body>");
            content.append("终于回来了。。。。");
            content.append("</body>");
            content.append("</html>");
            int size = content.toString().getBytes().length;
            StringBuilder responseInfo = new StringBuilder();
            String blank = " ";
            String CRLF = "\r\n";
            //返回
            //1、响应状态行: HTTP/1.1 200 OK
            responseInfo.append("HTTP/1.1").append(blank);
            responseInfo.append(200).append(blank);
            responseInfo.append("OK").append(CRLF);
            //2、响应头(最后一行空行):
            responseInfo.append("Date:").append(new Date()).append(CRLF);
            responseInfo.append("Server:").append("shsxt Server/0.01;charset=GBK").append(CRLF);
            responseInfo.append("Content-type:test/html").append(CRLF);
            responseInfo.append("Content-lenth:").append(size).append(CRLF).append(CRLF);
            //3、正文
            responseInfo.append(content.toString());
            //4、写出到客户端
            bw.write(responseInfo.toString());
            bw.flush();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("客户端错误");
        }
    }
    //停止服务
    public void stop() {

    }
}

使用浏览器的RESTer插件模拟GET和POST操作,却一直失败得不到返回的结果。

1.client没有关闭,应该关闭,在 bw.flush(); 后一行添加 client.close();
2.响应头的 Content-type 中应该是 text/html 而不是 test/html
3.charset=GBK 字符编码应该放在响应头的 Content-type 中,不应放在 server 中,还有,现在一般都是UTF-8编码,要确认你使用GBK是可以的才行

老铁,我研究了很久,终于找到了毛病,并不是那个人说的client没关闭,我这里看不到你的全部代码,我的没有响应的问题在于读取到输入流的时候,我用了while(int Len=is.read(flush)!=-1)的方式,所以导致后面响应不出去,后面换成全部一次性读取出来,就有响应了,**重点来了,你的错误在于响应头拼接那里的Content-lenth,是length不是lenth,所以说那个说和流关闭有关的就是没有自己研究过的,流关不关影响的是最后的空间问题,不会影响运行的结果**

额...虽然确实有lenth的问题,但是client不关闭确实会收不到响应