0

我正在用 Java 创建一个多线程 Web 服务器(例如本地主机:http://127.0.0.1)。我的问题是,如何从 Web 服务器上的客户端读取 POST 请求 HTTP/1.1?

以下代码适用于 GET 请求,但我想知道如何获取 POST 请求中的属性:

void get(Socket socket) throws IOException {
    byte[] buffer = new byte[BUFFER_SIZE];
    if (buffer[0] == (byte)'G' &&
        buffer[1] == (byte)'E' &&
        buffer[2] == (byte)'T' &&
        buffer[3] == (byte)' ') {
    //READ FOLLOWING OF ? in header EX: Get /?ABC=XYZ 
    }
}
4

1 回答 1

1

Where is the buffer coming from? Consider using (buffered) socket.getInputStream() and reading byte-by-byte (actually character by character). Then once you read GET/POST you can continue reading the rest of the header.

BTW any reason to implement HTTP where so many HTTP servers and servlet containers are available, ready to be embedded? Remember that HTTP is surprisingly complex protocol...

于 2012-10-29T21:09:19.547 回答