Retrieve content from an HTTP request even if there is no contentlength header

Testing with a client that sends me an HTTP request without a content length header but has content.

How to extract this content without using the contentlength header?

+3
source share
3 answers

I kept the original answer for completeness, but I just looked at the HTTP RFC (2616) section 4.3:

Content-Length Transfer-Encoding . , ( 5.1.1) . ; -, .

, , Transfer-Encoding ( , 400 , 411 ( " " )), , Transfer-Encoding:)

, API ( HTTP API), - , , API (.. ).

, .


, , ( ).

(, ByteArrayOutputStream , , ), InputStream.read -1. :

byte[] buffer = new byte[8192];
ByteArrayOutputStream output = new ByteArrayOutputStream();
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1)
{
    output.write(buffer, 0, bytesRead);
}
// Now use the data in "output"

EDIT: , . API HTTP , , .

, (, , , ) - , , , TCP . - , .

" ", ...

+4

, , . , .

Content-Length:, :

  • Transfer-Encoding: chunked

, , :

POST /some/path HTTP/1.1
Host: www.example.com
Content-Type: text/plain
Transfer-Encoding: chunked

25
This is the data in the first chunk

1C
and this is the second one

3
con
8
sequence
0

( )

  • : , CRLF, , CRLF
  • ,
  • , HTTP
  • , HTTP- CRLF
+3

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


All Articles