如何防止HttpClient发送Connection头

时间:2017-11-21 11:04:22

标签: c# http-headers dotnet-httpclient

HttpClient将此作为' Keep-Alive'默认情况下。我已经能够将其设置为“关闭”#39;使用httpClient.DefaultRequestHeaders.ConnectionClose = true;,但我如何完全省略它?

1 个答案:

答案 0 :(得分:10)

我之前已经看过这个问题,据我所知,没有人(not even the great Jon Skeet)想出办法避免在Connection或{HttpClient中发送HttpWebRequest标题{1}}世界没有完全重新发明轮子。它发生在杂草深处的某个地方。

ConnectionClose可以为空,但在HttpClient.DefaultRequestHeadersHttpRequestMethod.Headers上将其设置为空仍会导致发送Connection: Keep-Alive标头。

总结:

  • 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不可为空)