How to prevent HttpClient from sending the Connection header

后端 未结 1 1544
北荒
北荒 2021-01-11 17:07

HttpClient includes this as \'Keep-Alive\' by default. I\'ve been able to set it to \'Close\' using httpClient.DefaultRequestHeaders.ConnectionClose = true; but

相关标签:
1条回答
  • 2021-01-11 17:37

    I've seen this asked before and to my knowledge no one (not even the great Jon Skeet) has come up with a way to avoid sending the Connection header in the HttpClient or HttpWebRequest worlds without completely reinventing the wheel. It's happening somewhere very deep down in the weeds.

    ConnectionClose is nullable, but setting it to null on both HttpClient.DefaultRequestHeaders AND HttpRequestMethod.Headers still results in a Connection: Keep-Alive header being sent.

    To summarize:

    • HttpRequestHeaders.ConnectionClose = true => Connection: close
    • HttpRequestHeaders.ConnectionClose = false => Connection: Keep-Alive
    • HttpRequestHeaders.ConnectionClose = null => Connection: Keep-Alive

    • HttpWebRequest.KeepAlive = true => Connection: Keep-Alive

    • HttpWebRequest.KeepAlive = false => Connection: close

    (HttpWebRequest.KeepAlive is not nullable)

    0 讨论(0)
提交回复
热议问题