c# - 如何防止 HttpClient 发送连接 header

标签 c# http-headers dotnet-httpclient

HttpClient 默认将其作为“Keep-Alive”包含在内。我已经能够使用 httpClient.DefaultRequestHeaders.ConnectionClose = true; 将其设置为“关闭”,但如何完全忽略它?

最佳答案

我以前见过这个问题,据我所知,没有人 ( not even the great Jon Skeet ) 想出一种方法来避免在 HttpClient 中发送 Connection header 或 HttpWebRequest 世界,无需完全重新发明轮子。它发生在杂草深处的某个地方。

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

总结:

  • HttpRequestHeaders.ConnectionClose = true => 连接:关闭
  • HttpRequestHeaders.ConnectionClose = false => Connection: Keep-Alive
  • HttpRequestHeaders.ConnectionClose = null => Connection: Keep-Alive

  • HttpWebRequest.KeepAlive = true => 连接:Keep-Alive

  • HttpWebRequest.KeepAlive = false => 连接:关闭

(HttpWebRequest.KeepAlive 不可为空)

关于c# - 如何防止 HttpClient 发送连接 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47411524/

相关文章:

c# - "skipped loading symbols for ngen binary"用于 C# dll

java - 如何将基于多个条件的 header 添加到 HttpRequest Java 11?

c# - 为什么 HttpRequestHeaders.GetValues(string) 返回的 IEnumerable 只有 1 个字符串?

iphone - CFNetwork 读取 http header Transfer-Encoding Identity 但 wireshark 显示分块

c# - ASP.NET MVC5 Html.Beginform 不调用操作方法

c# - 在 C# 中使用 Linq 删除 sql 行

c# - HttpClient.PutAsync- "Entity only allows writes with a JSON Content-Type header"

c# - HttpClient.PostAsync 无法识别的响应

c# - Graphics.DrawPath 和 LinearGradientBrush 问题

c# - 如何在我的集成测试中使用 System.Net.Http.HttpClient 处理异步异常?