保持 HttpHandler 活动/刷新中间数据
背景:我们的一台 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以写入输出流,这将执行您想要的操作。
查看在 ASP.NET 中流式传输文件的最佳方式
You can write to the output stream and this will do what you want.
Check out Best way to stream files in ASP.NET
实际上,这毕竟工作得很好:
我必须使用终端程序来连接,但结果证明没问题。
值得一提的是,ASP 添加了一个
Transfer-Encoding: chunked
header 改变了数据发送的方式:Actually, this works fine after all:
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: