Buffered HTTP POST Reader

Looking for a little help, I have currently written an HTTP server. It currently handles GET requests very well. However, using POST, the buffered reader seems to hang. When the request stops, the rest of the input stream is read through a buffer reader. I found a few things on Google. I tried changing the CRLF and protocol version from 1.1 to 1.0 (browsers automatically make requests like 1.1). Any ideas or help would be appreciated. Thanks

+3
source share
4 answers

It's not safe! But it shows how to get POST data in the input stream after the initial HTTP headers.

POST, "example = true & bad = false" ..

private HashMap hashMap = new HashMap();
private StringBuffer buff = new StringBuffer();
private int c = 0;
private String[] post;    public PostInputStream(InputStream in) {

    try {
        //Initalizes avaliable buff
        if (in.available() != 0) {
            this.buff.appendCodePoint((this.c = in.read()));
            while (0 != in.available()) {
                //Console.output(buff.toString());
                buff.appendCodePoint((this.c = in.read()));
            }

            this.post = buff.toString().split("&");

            for (int i = 0; i < this.post.length; i++) {
                String[] n = this.post[i].split("=");
                if (n.length == 2) {
                    hashMap.put(URLDecoder.decode(n[0], "UTF-8"), URLDecoder.decode(n[1], "UTF-8"));
                } else {
                    Console.error("Malformed Post Request.");
                }
            }
        } else {
            Console.error("No POST Data");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
+1

, . , , HTTP, , .

BufferedReader, . , BufferedReader , ( POST ), ( ).

POST InputStream

  • , "\ r", "\n"

  • , "Content-Length:", .

  • , , .

  • , Content-Length.

.

+7
+2

karoroberts, , POST. BufferedReader.

Content-Length , char POST:

char [] buffer = new char [contentLength]; request.read();

BufferedReader. POST , : String.valueOf(buffer);

. BufferedReader.read int , , Content-Length.

0

Source: https://habr.com/ru/post/1720311/


All Articles