C# 保持HttpHandler活动/刷新中间数据
背景:我们的一个GPRS设备通过代理连接到通用处理器时出现问题。尽管处理程序在返回后立即关闭连接,但代理会保持连接打开,这是设备所不期望的 我的问题:在处理程序返回其数据后,出于测试目的(为了模拟代理的行为),是否可以在短时间内保持连接的活动状态 例如,这不起作用:C# 保持HttpHandler活动/刷新中间数据,c#,asp.net,http,keep-alive,generic-handler,C#,Asp.net,Http,Keep Alive,Generic Handler,背景:我们的一个GPRS设备通过代理连接到通用处理器时出现问题。尽管处理程序在返回后立即关闭连接,但代理会保持连接打开,这是设备所不期望的 我的问题:在处理程序返回其数据后,出于测试目的(为了模拟代理的行为),是否可以在短时间内保持连接的活动状态 例如,这不起作用: public class Ping : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Respons
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;
}
}
}
公共类Ping:IHttpHandler
{
公共void ProcessRequest(HttpContext上下文)
{
context.Response.BufferOutput=false;
context.Response.ContentType=“text/plain”;
context.Response.WriteLine(“你好”);
context.Response.Flush()
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();
byte[]buffer=new byte[1实际上,这毕竟可以正常工作:
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);
}
}
公共类Ping:IHttpHandler
{
公共void ProcessRequest(HttpContext上下文)
{
context.Response.BufferOutput=false;
context.Response.ContentType=“text/plain”;
context.Response.WriteLine(“HELLO”);//谢谢。事实证明,我实际上不需要这样做,但你的建议还是奏效了。看来简单的刷新毕竟还是如预期的那样有效。