作者:明恋夏日2 | 来源:互联网 | 2023-02-12 12:24
我有一个跨请求共享的静态httpclient,我想为它添加一个自定义标头.
httpClient.DefaultRequestHeaders.Add("customHeader", somevalue.ToString());
但我注意到,在每个请求中,该值都会添加到我打算在每个请求上替换的标头中.我尝试删除标头,如果它已经存在并再次添加,但它在负载测试时给我一个错误.
if (httpClient.DefaultRequestHeaders.Contains("customHeader"))
{
httpClient.DefaultRequestHeaders.Remove("customHeader");
}
httpClient.DefaultRequestHeaders.Add("customHeader",somevalue.ToString());
错误 -
System.ArgumentException: An item with the same key has already been added.
System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
System.ArgumentNullException: Value cannot be null.
如何更新每个请求的自定义标头值?