如何做出 keep-alive header 回应?

How to make keep-alive header response?

我尝试使用 GET 制作简单的 Web API 应用程序,POST.When 我响应 GET 请求:

 {
    "status": "OK",
    "headers":
    {
        "Date": "Fri, 18 Aug 2017 16:50:38 GMT",
        "Transfer-Encoding": "chunked",
        "Connection": "close",
        "Content-Type": "application/json; charset=utf-8",
        "Server": "Kestrel"
    },
    "body": "....",
    "code": 200,
    "protocol": "HTTP/1.1"
}

但我想制作 "Connection": "keep-alive",而不是 "Connection": "close",所以我更改了配置。

Startup.cs:

 public void Configure(IApplicationBuilder app,
                       IHostingEnvironment env,
                       ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();
    app.UseMvc();
    app.UseSession();
    app.Run(async (context) =>
    {
        context.Response.Headers[HeaderNames.Connection] = "Keep-Alive";
    });                    
}

但是,我收到了 "Connection": "close" 回复。

知道如何更改为 "keep-alive" 设置吗?

HTTP/1.1 定义了 "close" 连接选项,用于发送方发出信号表示连接将在响应完成后关闭。例如,请求或响应 header 字段中的 Connection: close 表示当前 request/response 完成后不应将连接视为“持久” .

如果您正在尝试实现持久连接,您可能需要查看 websockets。