如何防止HttpClient发送连接头 [英] How to prevent HttpClient from sending the Connection header
问题描述
HttpClient默认将其包括为保持活动。我已经能够使用 httpClient.DefaultRequestHeaders.ConnectionClose = true;
将其设置为关闭,但是如何完全省略它?
HttpClient includes this as 'Keep-Alive' by default. I've been able to set it to 'Close' using httpClient.DefaultRequestHeaders.ConnectionClose = true;
but how do I omit it altogether?
推荐答案
我以前从未见过这个问题,据我所知(甚至不是伟大的Jon Skeet )都想出一种避免在 HttpClient $ c中发送
Connection
标头的方法$ c>或 HttpWebRequest
的世界,而无需完全重新发明轮子。
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
可以为空,但是在两个< HttpClient.DefaultRequestHeaders
和 HttpRequestMethod.Headers
仍会导致 Connection:保持活动$
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.
总结:
-
HttpRequestHeaders.ConnectionClose = true
=>连接:close
-
HttpRequestHeaders.ConnectionClose = false
=>连接:保持活动
-
HttpRequestHeaders.ConnectionClose = null
=>连接:保持活动
HttpRequestHeaders.ConnectionClose = true
=>Connection: close
HttpRequestHeaders.ConnectionClose = false
=>Connection: Keep-Alive
HttpRequestHeaders.ConnectionClose = null
=>Connection: Keep-Alive
HttpWebRequest.KeepAlive = true
=> 连接:保持活动
( HttpWebRequest.KeepAlive
不能为空)
这篇关于如何防止HttpClient发送连接头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!