添加连接:在 ASP.net 中,保持活动标头不会返回给客户端

发布于 2025-01-10 07:00:09 字数 2313 浏览 4 评论 0原文

简短版本

我正在添加响应标头:

Connection: keep-alive

但它不在响应中。

长版本

我试图添加 标头HttpResponse 在 ASP.net 中:

public void ProcessRequest(HttpContext context)
{
    context.Response.CacheControl = "no-cache";
    context.Response.AppendHeader("Connection", "keep-alive");
    context.Response.AppendHeader("AreTheseWorking", "yes");
    context.Response.Flush();
}

当响应返回到客户端(例如 Chrome、Edge、Internet Explorer、Postman)时,Connection 标头丢失:

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Transfer-Encoding: chunked
Expires: -1
Server: Microsoft-IIS/10.0
AreTheseWorking: yes
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Sat, 26 Feb 2022 16:29:17 GMT

我做错了什么?

还有额外的 Chatter

除了尝试 之外, AppendHeader

context.Response.AppendHeader("Connection", "keep-alive"); //preferred

我也尝试过AddHeader (其存在“是为了与早期版本的 ASP 兼容”)

context.Response.AddHeader("Connection", "keep-alive"); // legacy

我也尝试过 Headers.Add

context.Response.Headers.Add("Connection", "keep-alive"); //requires IIS 7 and integrated pipeline

我做错了什么?

奖励: 假设动机对于这个问题

Short Version

I'm adding the response header:

Connection: keep-alive

but it's not in the resposne.

Long Version

I am trying to add a header to an HttpResponse in ASP.net:

public void ProcessRequest(HttpContext context)
{
    context.Response.CacheControl = "no-cache";
    context.Response.AppendHeader("Connection", "keep-alive");
    context.Response.AppendHeader("AreTheseWorking", "yes");
    context.Response.Flush();
}

And when the response comes back to the client (e.g. Chrome, Edge, Internet Explorer, Postman), the Connection header is missing:

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Transfer-Encoding: chunked
Expires: -1
Server: Microsoft-IIS/10.0
AreTheseWorking: yes
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Sat, 26 Feb 2022 16:29:17 GMT

What am I doing wrong?

Bonus Chatter

In addition to trying AppendHeader:

context.Response.AppendHeader("Connection", "keep-alive"); //preferred

I also tried AddHeader (which exists "for compatibility with earlier versions of ASP"):

context.Response.AddHeader("Connection", "keep-alive"); // legacy

I also tried Headers.Add:

context.Response.Headers.Add("Connection", "keep-alive"); //requires IIS 7 and integrated pipeline

What am i doing wrong?

Bonus: hypothetical motivation for the question

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

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

发布评论

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

评论(1

瑕疵 2025-01-17 07:00:09

默认情况下,ASP.net 中不允许 keep-alive

为了允许它,您需要添加一个选项< /a> 到您的 web.config

web.config

<configuration>
    <system.webServer>
        <httpProtocol allowKeepAlive="true" />
    </system.webServer>
</configuration>

这对于 服务器发送事件:

public void ProcessRequest(HttpContext context)
{
   if (context.Request.AcceptTypes.Any("text/event-stream".Contains))
   {
      //Startup the HTTP Server Send Event - broadcasting values every 1 second.
      SendSSE(context);
      return;
   }
}
private void SendSSE(HttpContext context)
{
   //Don't worry about it.
   string sessionId = context.Session.SessionID; //https://stackoverflow.com/a/1966562/12597

   //Setup the response the way SSE needs to be
   context.Response.ContentType = "text/event-stream";
   context.Response.CacheControl = "no-cache";
   context.Response.AppendHeader("Connection", "keep-alive");
   context.Response.Flush();

   while (context.Response.IsClientConnected)
   {
      System.Threading.Thread.Sleep(1000);
 
      String data = DateTime.Now.ToString();
      context.Response.Write("data: " + data + "\n\n");
      context.Response.Flush();
   }
}

By default keep-alive is not allowed in ASP.net.

In order to allow it, you need to add an option to your web.config:

web.config:

<configuration>
    <system.webServer>
        <httpProtocol allowKeepAlive="true" />
    </system.webServer>
</configuration>

This is especially important for Server-Send Events:

public void ProcessRequest(HttpContext context)
{
   if (context.Request.AcceptTypes.Any("text/event-stream".Contains))
   {
      //Startup the HTTP Server Send Event - broadcasting values every 1 second.
      SendSSE(context);
      return;
   }
}
private void SendSSE(HttpContext context)
{
   //Don't worry about it.
   string sessionId = context.Session.SessionID; //https://stackoverflow.com/a/1966562/12597

   //Setup the response the way SSE needs to be
   context.Response.ContentType = "text/event-stream";
   context.Response.CacheControl = "no-cache";
   context.Response.AppendHeader("Connection", "keep-alive");
   context.Response.Flush();

   while (context.Response.IsClientConnected)
   {
      System.Threading.Thread.Sleep(1000);
 
      String data = DateTime.Now.ToString();
      context.Response.Write("data: " + data + "\n\n");
      context.Response.Flush();
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文