作者:Perz | 来源:互联网 | 2023-05-19 01:09
InavalidationsetupIwanttochangethevalueofaheaderofaHttpRequestMessage.在验证设置中,我想更改Htt
In a validation setup I want to change the value of a header of a HttpRequestMessage
.
在验证设置中,我想更改HttpRequestMessage的标头的值。
In a HttpClientHandler
I have the following code:
在HttpClientHandler中,我有以下代码:
protected override async Task
SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
//some condition when to alter the header
//does not work: value is read only
request.Headers.Single(c => c.Key == "FooHeader").Value =
new List({"aha!"});
//does not work: cannot apply indexer
request.Headers["FooHeader"] = "aha!"
//does work but seems a bit overkill, besides I need to check if it exists
request.Headers.Remove("FooHeader");
request.Headers.Add("FooHeader", "aha!");
}
Is there a more intuitive way to achieve this?
有更直观的方法来实现这一目标吗?
1 个解决方案