本文介绍了如何在没有传输编码的情况下发送HTTP响应:分块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个响应Twilio API的Java Servlet.看来Twilio不支持我的响应正在使用的分块传输.如何避免使用Transfer-Encoding: chunked
?
I have a Java Servlet that responds to the Twilio API. It appears that Twilio does not support the chunked transfer that my responses are using. How can I avoid using Transfer-Encoding: chunked
?
这是我的代码:
// response is HttpServletResponse
// xml is a String with XML in it
response.getWriter().write(xml);
response.getWriter().flush();
我正在使用Jetty作为Servlet容器.
I am using Jetty as the Servlet container.
推荐答案
尝试设置内容长度,然后再写入流.不要忘记根据正确的编码来计算字节数,例如:
Try setting the Content-length before writing to the stream. Don't forget to calculate the amount of bytes according to the correct encoding, e.g.:
final byte[] content = xml.getBytes("UTF-8");
response.setContentLength(content.length);
response.setContentType("text/xml"); // or "text/xml; charset=UTF-8"
response.setCharacterEncoding("UTF-8");
final OutputStream out = response.getOutputStream();
out.write(content);
这篇关于如何在没有传输编码的情况下发送HTTP响应:分块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!