简体   繁体   English

在Java中接收POST HTTP请求

[英]Receive POST HTTP request in java

I am creating a multi-threadrd web server (eg local host:http://127.0.0.1) in Java. 我正在用Java创建一个多线程的Web服务器(例如本地主机:http://127.0.0.1)。 My question is, how I can read POST request HTTP/1.1 from clients at web server? 我的问题是,如何从Web服务器上的客户端读取POST请求HTTP / 1.1?

Following code works for GET request, but I am wondering how I can get the attributes in POST request: 以下代码适用于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 
    }
}

Where is the buffer coming from? buffer从哪里来? Consider using (buffered) socket.getInputStream() and reading byte-by-byte (actually character by character). 考虑使用(缓冲的) socket.getInputStream()并逐字节读取(实际上是逐字符读取)。 Then once you read GET / POST you can continue reading the rest of the header. 然后,一旦您阅读GET / POST ,就可以继续阅读标题的其余部分。

BTW any reason to implement HTTP where so many HTTP servers and servlet containers are available, ready to be embedded? 顺便说一句,在拥有如此多的HTTP服务器和servlet容器并准备好嵌入的情况下,有什么理由实施HTTP? Remember that HTTP is surprisingly complex protocol... 请记住,HTTP是令人惊讶的复杂协议...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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