保持 HttpHandler 活动/刷新中间数据

发布于 2024-12-20 10:51:44 字数 881 浏览 4 评论 0原文

背景:我们的一台 GPRS 设备通过代理连接到通用处理程序时遇到问题。尽管处理程序在返回后立即关闭连接,但代理仍保持连接打开,这是设备不期望的。

我的问题:出于测试目的(为了模仿代理的行为),在处理程序返回其数据后,是否可以在短时间内保持连接处于活动状态?

例如,这工作:

public class Ping : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.BufferOutput = false;

        context.Response.ContentType = "text/plain";
        context.Response.WriteLine("HELLO");
        context.Response.Flush();  // <-- this doesn't send the data

        System.Threading.Thread.Sleep(10000);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

[编辑]

好吧,实际上,它按预期工作。问题是 Firefox 和 Fiddler 都会延迟显示原始数据,直到连接关闭。

如果将 Response.BufferOutput 设置为 false,并且我使用终端程序进行连接,我会立即获取数据,并且连接将保持打开状态 10 秒。

Background: we are having issues with one of our GPRS devices connecting through a proxy to a generic handler. Although the handler closes the connection immediately after returning, the proxy keeps the connection open, which the device does not expect.

My question: is it possible, for testing purposes (in order to mimic the proxy's behavior), to keep the connection alive for some short time, after a handler has returned its data?

For example, this does not work:

public class Ping : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.BufferOutput = false;

        context.Response.ContentType = "text/plain";
        context.Response.WriteLine("HELLO");
        context.Response.Flush();  // <-- this doesn't send the data

        System.Threading.Thread.Sleep(10000);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

[Edit]

Ok, actually, it works as expected. The problem is that both Firefox and Fiddler delay showing the raw data until the connection is closed.

If Response.BufferOutput is set to false, and I use a terminal program to connect, I get the data immediately, and the connection remains open for 10s.

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

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

发布评论

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

评论(2

错々过的事 2024-12-27 10:51:44

您可以写入输出流,这将执行您想要的操作。

byte [] buffer = new byte[1<<16] // 64kb
int bytesRead = 0;
using(var file = File.Open(path))
{
   while((bytesRead = file.Read(buffer, 0, buffer.Length)) != 0)
   {
        Response.OutputStream.Write(buffer, 0, bytesRead);
         // can sleep here or whatever
   }
}
Response.Flush();
Response.Close();
Response.End();

查看在 ASP.NET 中流式传输文件的最佳方式

You can write to the output stream and this will do what you want.

byte [] buffer = new byte[1<<16] // 64kb
int bytesRead = 0;
using(var file = File.Open(path))
{
   while((bytesRead = file.Read(buffer, 0, buffer.Length)) != 0)
   {
        Response.OutputStream.Write(buffer, 0, bytesRead);
         // can sleep here or whatever
   }
}
Response.Flush();
Response.Close();
Response.End();

Check out Best way to stream files in ASP.NET

§对你不离不弃 2024-12-27 10:51:44

实际上,这毕竟工作得很好:

public class Ping : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.BufferOutput = false;

        context.Response.ContentType = "text/plain";
        context.Response.WriteLine("HELLO"); // <-- data is sent immediately

        System.Threading.Thread.Sleep(10000);
    }
}

我必须使用终端程序来连接,但结果证明没问题。

值得一提的是,ASP 添加了一个 Transfer-Encoding: chunked header 改变了数据发送的方式:

每个块的大小在块本身之前发送,以便
客户端可以知道它何时完成接收该块的数据。
数据传输由长度为零的最后一个块终止。

Actually, this works fine after all:

public class Ping : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.BufferOutput = false;

        context.Response.ContentType = "text/plain";
        context.Response.WriteLine("HELLO"); // <-- data is sent immediately

        System.Threading.Thread.Sleep(10000);
    }
}

I had to use a terminal program to connect, but then it turned out to be ok.

One thing that should be mentioned is that ASP adds a Transfer-Encoding: chunked header in this case, which changes the way data is sent:

The size of each chunk is sent right before the chunk itself so that a
client can tell when it has finished receiving data for that chunk.
The data transfer is terminated by a final chunk of length zero.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文