问题描述
当我通过Jersey ReST客户端发送POST请求时,它会自动使用标头传输编码:[chunked] 。
When i send a POST request through Jersey ReST client it's automatically using Header transfer-encoding: [chunked].
有没有办法强制使用内容长度而不是转码。?
Is there any way to force use of content-length: instead of transfer-encoding.?
WebTarget webTarget = client.target(connection.getServerUrl());
Invocation.Builder builder = webTarget.request(MediaType.APPLICATION_XML);
Response response = builder.post(Entity.xml(requestBroker));
添加Content-Length属性后,行为也相同
After adding Content-Length property too the behavior is same
WebTarget webTarget = client.target(connection.getServerUrl());
Invocation.Builder builder = webTarget.request(MediaType.APPLICATION_XML);
Entity entity = Entity.xml(requestBroker);
client.property("Content-Length", entity.toString().getBytes().length);
Response response = builder.post(Entity.xml(requestBroker));
推荐答案
HTTP 1.1版本以后chunked transfer encoding是POST的默认值,在此数据中作为块发送,因此发送者可以在知道该内容的总大小之前开始发送动态生成的内容。每个块的大小在块本身之前发送,以便接收器可以告知它何时完成接收该块的数据。数据传输由最后一个长度为零的块终止。
HTTP 1.1 version onwards chunked transfer encoding is default for POST, in this data is sent as chunks and hence the senders can begin transmitting dynamically-generated content before knowing the total size of that content. The size of each chunk is sent right before the chunk itself so that the receiver can tell when it has finished receiving data for that chunk. The data transfer is terminated by a final chunk of length zero.
在发送POST请求之前设置Content-Length标头。但这仅适用于http 1.0,当您设置内容长度时,如果发布请求数据大小超过内容长度,则收到的数据将被截断。
Set the Content-Length header before sending your POST request. But this will work only in http 1.0, and when you set the content length, and if the post request data size is more than the content length then the data received will be truncated.
在HTTP协议的1.1版中,分块传输机制被认为始终并且无论如何都是可接受的,即使未在TE(传输编码)请求头字段中列出,并且在使用时与其他传输机制一起,应始终最后应用于传输的数据,并且永远不会超过一次。来源维基百科 -
In the version 1.1 of the HTTP protocol, the chunked transfer mechanism is considered to be always and anyways acceptable, even if not listed in the TE (transfer encoding) request header field, and when used with other transfer mechanisms, should always be applied last to the transferred data and never more than one time. Source Wikipedia - Chunked Transfer Encoding
在响应中,我们可以通过使用response.setBufferSize()在响应时设置BufferSize来避免Transfer-Encoding。但是如果我们的响应大小超出bufferSize,它将回退到Transfer-Encoding:Chunked。
Whereas in the response, we can avoid Transfer-Encoding by setting the BufferSize on response using response.setBufferSize(). But if our response size goes beyond the bufferSize it would fallback to Transfer-Encoding: Chunked.
更多信息:
希望它有帮助!
这篇关于如何在没有传输编码的情况下发送POST请求:从Jersey ReST Client 2.22.2中分块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!