如何使keep-alive头响应?

时间:2017-08-18 17:02:14

标签: c# asp.net-core http-headers keep-alive asp.net-core-webapi

我尝试使用GET和POST制作简单的Web API应用程序。当我响应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"设置吗?

1 个答案:

答案 0 :(得分:0)

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

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