是否可以添加值为空/缺少的标头?
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
答案 0 :(得分:1)
那不是。它违背了HTTP protocol standard。
4.2邮件标题
HTTP标头字段,包括通用标头(第4.5节), request-header(第5.3节),response-header(第6.2节)和 entity-header(第7.1节)字段,遵循相同的通用格式 RFC 822 [9]的3.1节中给出的。每个标题字段包含 名称后面跟冒号(“:”)和字段值。
答案 1 :(得分: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);
交叉手指,以便服务器喜欢这个请求。
答案 2 :(得分: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.