简体   繁体   中英

Receive POST HTTP request in java

I am creating a multi-threadrd web server (eg local host:http://127.0.0.1) in Java. My question is, how I can read POST request HTTP/1.1 from clients at web server?

Following code works for GET request, but I am wondering how I can get the attributes in POST request:

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 
    }
}

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM