Content Headers Delete Crash for Authorization String

The following test fails inexplicably:

    [Test]
    public void CrazyAssHttpRequestMessageTest()
    {
        var subject = new HttpRequestMessage()
                          {
                              Method = HttpMethod.Get,
                              Content = new StringContent("some content")
                          };
        subject.Content.Headers.Remove("Authorization");
    }

The exceptions are:

System.InvalidOperationException: Invalid header name. Verify request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects.

Why? Any other header seems to work fine, replace authorization with something else, and everything is fine.

+4
source share
1 answer

HttpContentHeaders HTTP- - , . , , .

, request.Content.Headers.

​​ , "Content-Type" HttpRequestHeaders HttpResponseHeaders, , TryAddWithoutValidation. , Contains() , . , , HttpHeaders.TryGetValues request.Content.Headers.Any(x => x.Key == "Authorization").

, ( ), . HttpContentHeaders.ContentType.

+4

Source: https://habr.com/ru/post/1546340/


All Articles