gpt4 book ai didi

asp.net - MVC Controller 返回分块内容编码

转载 作者:行者123 更新时间:2023-12-04 08:44:46 27 4
gpt4 key购买 nike

所以我有一个MVC项目。这个 MVC 项目包含一个 Controller ,它需要将内容流式传输回客户端。当流式传输开始时,无法确定内容长度(它是即时计算的)。所以我打开 HttpContext.Current.Response.OutputStream,并开始定期写入和刷新(我已经禁用了缓冲输出,并附加了适当的 http header ):

while (some condition){

HttpContext.Current.Response.OutputStream.Write(buffer, 0, buffer.Length);
HttpContext.Current.Response.Flush();
}

如果我然后强制流关闭:
HttpContext.Current.Response.Close();

它没有正确结束分块内容(它不会在末尾附加一个长度为 0 的块,以向客户端指示 EOF)。

如果我更优雅地关闭输出流:
HttpContext.Current.Response.End();

或者
HttpContext.Current.ApplicationInstance.CompleteRequest();

它正确地关闭了流(附加到末尾的零长度块),但我收到应用程序抛出的异常,表明它无法将 HTTP header 插入输出流,因为流已经写入!

在这两种情况下,Controller 都会继续返回 null(或 EmptyActionResult)。

我假设异常是由于 MVC 堆栈要求在 Controller 完成执行后每个 ActionResult 设置 HTTP header 引起的。如果是这种情况,如何在 MVC 中实现分块流?

提前致谢!

编辑 :抛出的确切异常是:
Uncaught Exception: System.Web.HttpException (0x80004005): Server cannot set status after HTTP headers have been sent.
at System.Web.Http.WebHost.HttpControllerHandler.EndProcessRequest(IAsyncResult result)
at System.Web.Http.WebHost.HttpControllerHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

最佳答案

我找到了解决方案。

HTTP 1.1 标准规定,只有在请求模式为 keep-alive 时,chunked encoding 必须以 0 长度的 chunk 结束

在keep-alive 模式下,客户端/服务器之间的连接对于多个请求/响应是持久的。在这种情况下,块编码需要以零长度块结束,因为客户端没有其他方法可以知道前面的响应何时结束。

如果您指定“Connection: close”作为 header ,而不是“Connection: keep-alive”,则连接不会在请求之间持久化,客户端可以使用连接关闭作为响应终止的指示,并且不需要指示 EOF 的 0 长度块。

我刚刚决定使用以下方法手动关闭 HttpResponse:

HttpContext.Current.Response.Close();

虽然之前在代码中指定告诉客户端连接将在 EOF 时关闭。这解决了客户端没有收到 0 长度块的问题,因为现在客户端不需要它了。

关于asp.net - MVC Controller 返回分块内容编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17847335/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com