如何强制 IIS 发送响应标头而不发送响应正文并关闭连接

发布于 2024-10-06 19:45:07 字数 205 浏览 0 评论 0原文

我正在尝试使用 IIS 通过 HTTP 将动态生成的数据流式传输到客户端,并且连接必须长时间保持打开状态,并且服务器在执行耗时的操作时将定期向客户端发送状态更新。

这必须全部在一个请求中处理,但我使用的是 WebClient.OpenRead() 流,在发送标头之前无法打开该流。

如何强制 IIS 将标头发送到客户端,然后发送响应正文?

I am trying to stream dynamically generated data to a client over HTTP using IIS, and the connection has to remain open for a long period of time, and the server will send periodic status updates to the client while it is performing a time-consuming operation.

This MUST all be handled within ONE request, but I am using a WebClient.OpenRead() stream, which cannot be opened until the headers are sent.

How can I force IIS to send headers to the client, and later send a response body?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一页 2024-10-13 19:45:07

通常可以通过将 KeepAlive 设置为 true 并将 Expect 标头设置为“100 并继续”来实现此行为。通过这样做,服务器将发送结果代码为 100 的标头。

我不确定使用 WebClient 是否可以实现这一点。


使用 HttpWebRequest 来设置上述值。事实上,WebClient 没有做任何神奇的事情,只是使用 GET 来获取数据。下面是Reflector中调用OpenRead的代码:

try
    {
        request = this.m_WebRequest = this.GetWebRequest(this.GetUri(address));
        Stream responseStream = (this.m_WebResponse = this.GetWebResponse(request)).GetResponseStream();
        if (Logging.On)
        {
            Logging.Exit(Logging.Web, this, "OpenRead", responseStream);
        }
        stream2 = responseStream;
    }
    catch (Exception exception)
    {
     //

This behaviour is normally achievable by setting KeepAlive to true and setting Expect header to "100 and continue". By doing this, server will send the headers with result code 100.

I am not sure if this is possible using WebClient.


Use HttpWebRequest instead to be able to set the values above. In fact WebClient does nothing magical but using GET to get the data. Here is the code for calling OpenRead in Reflector:

try
    {
        request = this.m_WebRequest = this.GetWebRequest(this.GetUri(address));
        Stream responseStream = (this.m_WebResponse = this.GetWebResponse(request)).GetResponseStream();
        if (Logging.On)
        {
            Logging.Exit(Logging.Web, this, "OpenRead", responseStream);
        }
        stream2 = responseStream;
    }
    catch (Exception exception)
    {
     //
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文