HttpClient将此作为' Keep-Alive'默认情况下。我已经能够将其设置为“关闭”#39;使用httpClient.DefaultRequestHeaders.ConnectionClose = true;
,但我如何完全省略它?
答案 0 :(得分:10)
我之前已经看过这个问题,据我所知,没有人(not even the great Jon Skeet)想出办法避免在Connection
或{HttpClient
中发送HttpWebRequest
标题{1}}世界没有完全重新发明轮子。它发生在杂草深处的某个地方。
ConnectionClose
可以为空,但在HttpClient.DefaultRequestHeaders
和HttpRequestMethod.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
不可为空)