1

是否可以添加一个带有 value = blank/missing 的标题?

HttpRequestMessage.Headers.Add("header-name", string.Empty);

System.FormatException was unhandled by user code
  HResult=-2146233033
  Message=The format of value '' is invalid.
  Source=System.Net.Http
  StackTrace:
       at System.Net.Http.Headers.HttpHeaderParser.ParseValue(String value, Object storeValue, Int32& index)
       at System.Net.Http.Headers.HttpHeaders.ParseAndAddValue(String name, HeaderStoreItemInfo info, String value)
       at System.Net.Http.Headers.HttpHeaders.Add(String name, String value)
       at 
4

3 回答 3

1

那将是不。它违背了HTTP 协议标准

4.2 消息头

HTTP 头字段,包括 general-header(第 4.5 节)、request-header(第 5.3 节)、response-header(第 6.2 节)和 entity-header(第 7.1 节)字段,遵循与第 7.1 节中给出的相同的通用格式RFC 822 [9] 的 3.1。每个标头字段由一个名称后跟一个冒号(“:”)和字段值组成。

于 2014-02-19T05:46:19.713 回答
1

如果你想发送一个没有价值的疯狂头,使用TryAddWithoutValidation()方法,这里是一个例子:

HttpRequestMessage request = new HttpRequestMessage();
request.RequestUri = new Uri("http://kiewic.com");
var result = request.Headers.TryAddWithoutValidation("Authorization", string.Empty);

HttpClient client = new HttpClient();
var response = await client.SendAsync(request);

并交叉手指,以便服务器喜欢该请求。

于 2014-11-14T01:21:52.877 回答
0

您可以通过以下方式之一获取 HttpHeaders 的空实例:

var headers = new HttpClient().DefaultRequestHeaders;
var headers = new HttpResponseMessage().Headers;
var headers = new HttpRequestMessage().Headers;
You could also make your own class that extends HttpHeaders and use that.
于 2014-02-19T06:24:39.643 回答