content-length和chunked

content-length

When requested, the request field in different ways with different

If the request is a way to get or head of the value of this property can not be set or can be set to 0, otherwise it will report formatting error

If the requested mode is the post, it is necessary to set this property, the byte length is a length of the transmitted packet.

 

It does not support the keep alive before Http1.1. We can draw the following conclusions:

1, in Http 1.0 and previous versions, content-length field is optional.

2, version http1.1 and after. If you keep alive, and the content-length chunk must be a second election. If the non-keep alive, and http1.0 the same. content-length dispensable.

 

1, Content-Length, if present and valid, then the transfer length must be exactly the same message content. (After testing, if too short it will cut off too long will result in a timeout.)

2, if there is Transfer-Encoding (emphasis is chunked), you can not have in the Content-Length header, there will also be ignored.

3, if a short connection, directly connected to the server can be shut down to determine the length of the message transmission. (This is easy to understand)

 

When there is no Content-length, client how to determine whether the data has been received it?
1) static page or image: When a client to server requests a static page or a picture, the server can clearly know the content size, and then tell the client through the Content-length message header fields how much data needs to receive.
2) dynamic page: if a dynamic page, etc., the server is not possible to know in advance the size of the content, can then use a Transfer-Encoding: chunk mode to transmit the data. That aside, if you want to generate data to the client side, the server you need to use: in this way "Transfer-Encoding chunked" instead of Content-Length.
Transfer-encoding of the object using
     While generating data to the client side.
 
chunked encoding
1) the definition of
     Chunked transfer encoding (Chunked transfer encoding) data transfer mechanism is a Hypertext Transfer Protocol (HTTP) is allowed by the HTTP web server sent to the client application (typically a Web browser) may be divided into a plurality of data portions. Chunked transfer encoding only the HTTP protocol version 1.1 (HTTP / 1.1) in.
2) Description:
     Typically, data sent in the HTTP response message is transmitted to the entire, Content-Length header field indicates the length of the data. The length of the data is important, because the client needs to know where is the end of the response message and the beginning of the subsequent response message. However, using the chunked transfer encoding, data is divided into a series of data blocks, and send one or more blocks, so that the server can send data without prior knowledge of the total size of the transmitted content.

Guess you like

Origin www.cnblogs.com/suntp/p/8534745.html