禁止每个请求分块 [英] Disable chunking per request
问题描述
URLLoader
不能用于分块响应。从来没有从服务器接收任何分块响应。 我使用Glassfish来托管Servlet。我知道如何禁用整个服务器的分块,但这似乎是一个坏主意(是吗?)。
是否有标准的方法来禁用每个请求的分块?我打过电话 ServletResponse.setBufferSize(SOME_LARGE_VALUE)
,但令人惊讶的,这并不会影响服务器的使用分块的决定。
从 的HttpServlet#的doGet的javadoc的
():
$ block
$ b如果可能的话,设置
Content-Length
头(使用 ServletResponse.setContentLength(int)
方法),以允许servlet容器使用持久连接将其响应返回给客户端,提高性能。如果整个响应适合在响应缓冲区内,内容长度将被自动设置。 当使用HTTP 1.1分块编码(意味着响应有一个 Transfer-Encoding
头),不要设置 Content-Length
头。 b
$ b 所以,如果你设置了响应内容的长度那么它将不会以chunked编码发送。
response.setContentLength(contentLength);
// ...
Update :您还需要确保servlet不被< jsp:include>
或 RequestDispatcher的#包括()
。还参见它的javadoc :
...
ServletResponse
对象的路径元素和参数保持不变。包含的servlet不能更改响应状态码或设置标题;任何尝试进行更改都将被忽略。
...
I have a web service backed by a Java Servlet. The service is used by an older version of Flash. We discovered through some pain that in this version of Flash, URLLoader
won't work with chunked responses. Any chunked response is never received from the server.
I am using Glassfish to host the Servlet. I know how to disable chunking for the entire server, but that seems like a bad idea (is it?).
Is there a standard way to disable chunking per request? I tried calling ServletResponse.setBufferSize(SOME_LARGE_VALUE)
but surprising this did not affect the server's decision to use chunking.
From the javadoc of HttpServlet#doGet()
:
...
Where possible, set the
Content-Length
header (with theServletResponse.setContentLength(int)
method), to allow the servlet container to use a persistent connection to return its response to the client, improving performance. The content length is automatically set if the entire response fits inside the response buffer.When using HTTP 1.1 chunked encoding (which means that the response has a
Transfer-Encoding
header), do not set theContent-Length
header....
So, if you set the response content length beforehand, then it won't be sent in chunked encoding.
response.setContentLength(contentLength);
// ...
Update: You also need to make sure that the servlet isn't in turn been called by <jsp:include>
or RequestDispatcher#include()
. See also its javadoc:
...
The
ServletResponse
object has its path elements and parameters remain unchanged from the caller's. The included servlet cannot change the response status code or set headers; any attempt to make a change is ignored....
这篇关于禁止每个请求分块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!