Ingsuifon 2020-02-15 17:21 采纳率: 0%
浏览 450
已结题

浏览器访问本地手写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操作,却一直失败得不到返回的结果。

  • 写回答

3条回答 默认 最新

  • 王二小丷 2020-02-15 18:42
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算