【问题标题】:Open Source Java HTTP Codecs开源 Java HTTP 编解码器
【发布时间】:2019-06-05 12:15:49
【问题描述】:

我正在从头开始编写 Web 服务器。我需要一个 Http 编解码器,它可以将字符串请求(缓冲区)解码为 Http 对象,并将 http 对象编码为 Sting(缓冲区)。

我找到了三个编解码器,

  1. Apache 编解码器(不能使用它,因为它与其服务器编码结构紧密耦合)
  2. Netty 代码(不能使用它,因为它与其服务器编码结构紧密耦合)
  3. JDrupes 编解码器(存在一些并发问题)

但是这些都不能用于我的目的。还有其他我可以使用的编解码器吗?

【问题讨论】:

  • 为什么不能使用找到的编解码器?你的标准是什么?

标签: java codec


【解决方案1】:
class SimpleHttpsServer implements Runnable {
    Thread process = new Thread(this);
    private static int port = 3030;
    private String returnMessage;
    private ServerSocket ssocket;

    /************************************************************************************/
    SimpleHttpsServer() {
        try {
            ssocket = new ServerSocket(port);
            System.out.println("port " + port + " Opend");
            process.start();
        } catch (IOException e) {
            System.out.println("port " + port + " not opened due to  " + e);
            System.exit(1);
        }
    }

    /**********************************************************************************/
    public void run() {
        if (ssocket == null)
            return;
        while (true) {
            Socket csocket = null;
            try {
                csocket = ssocket.accept();
                System.out.println("New Connection accepted");
            } catch (IOException e) {
                System.out.println("Accept failed: " + port + ", " + e);
                System.exit(1);
            }
            try {
                DataInputStream dataInputStream = new DataInputStream(new BufferedInputStream(csocket.getInputStream()));
                PrintStream printStream = new PrintStream(new BufferedOutputStream(csocket.getOutputStream(), 1024),
                        false);
                this.returnMessage = "";
                InputStream inputStream = csocket.getInputStream();
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                // code to read and print headers
                String headerLine = null;
                while ((headerLine = bufferedReader.readLine()).length() != 0) {
                    System.out.println(headerLine);
                }
                // code to read the post payload data
                StringBuilder payload = new StringBuilder();
                while (bufferedReader.ready()) {
                    payload.append((char) bufferedReader.read());
                }

                System.out.println("payload.toString().length() " + payload.toString().length());
                if (payload.toString().length() != 1 || payload.toString().length() != 0) {
                    JSONObject jsonObject = null;
                    try {
                        jsonObject = new JSONObject(payload.toString());

                       // Handle here your string data and make responce 
                       // returnMessage this can store your responce message



                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    String httpResponse = "HTTP/1.1 200 OK\r\n\r\n" + this.returnMessage;
                    printStream.write(httpResponse.getBytes("UTF-8"));
                    printStream.flush();

                }else {
                    /*String httpResponse = "HTTP/1.1 200 OK\r\n\r\n";
                    outStream.write(httpResponse.getBytes("UTF-8"));
                    outStream.flush();*/
                }

                printStream.close();
                dataInputStream.close();
                // csocket.close();
                System.out.println("client disconnected");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /************************************************************************************/
    public static void main(String[] args) {
        new SimpleHttpsServer();
    }
}

也许这个对你有帮助

【讨论】:

  • 你想创建一个HTTP服务器然后这段代码可以工作你可以根据你的需要改变,这是工作代码
  • 我需要一种将 Http 编解码器添加到我的 Http 服务器的方法。我有没有编解码器的 Http Server 的工作版本
猜你喜欢
  • 2010-09-24
  • 2016-12-29
  • 1970-01-01
  • 2011-04-30
  • 2011-04-12
  • 2016-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多