HTTP protocol Literacy (IX) transfer-encoding=chunked mode of HTTP1.1 response

Source: Internet
Author: User
I. Background knowledge 1, chunked code

chunked transfer encoding (Chunked transfer encoding) is a data transfer mechanism that is provided only in the HTTP Protocol 1.1 version (http/1.1). The data in the previous HTTP response was sent together, and the Content-length field in the answer header identifies the length of the data so that the client knows the end of the reply message.

2. Benefits

For dynamically generated responses, the total length of content before the completion of the build is not known. Therefore, the generated content needs to be cached before the total length is populated to content-length, and then the entire data content is sent. This is not very flexible, and the use of block coding can be improved.
Chunked transfer encoding allows the server to send the message header field at the end. For example, add a hash signature to the header.
For compressed transport transmissions, you can compress one side of the transmission.

3. Format

If Transfer-encoding is chunked in the message header of HTTP, this is the way to encode it. Next, the number of unknown blocks is sent, and each block has a hexadecimal number at the beginning, indicating the size of the block and then CRLF ("\ r \ n"). Then there is the data itself, and after the data is finished, there will be a CRLF of two characters. In some implementations, there can be spaces between the block-size hexadecimal number and the CRLF.
The last chunk of the block size is 0, indicating the end of the data send. The last piece no longer contains any data, but you can send an optional trailer, including the message header field. The message ends with CRLF.

4. Case

Second, the code to send

Third, the code receives

HTTP protocol Literacy (IX) transfer-encoding=chunked mode of HTTP1.1 response

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.