如何在没有传输编码的情况下发送HTTP响应:分块? [英] How do I send an HTTP response without Transfer Encoding: chunked?

查看:192
本文介绍了如何在没有传输编码的情况下发送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响应:分块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆