What is the purpose of HttpHeaders.TryAddWithoutValidation?

In the System.Net.Http.Headers namespace, what is the difference between HttpHeaders.TryAddWithoutValidation and HttpHeaders.Add ?

In particular, what check is performed when calling the Add method? The documentation for Add () simply states:

"The value of the header will be analyzed and verified."

+5
source share
2 answers

Using a Reflector, this is what the TryAddWithoutValidation method does :

if (!this.TryCheckHeaderName(name))
{
    return false;
}
if (value == null)
{
    value = string.Empty;
}
AddValue(this.GetOrCreateHeaderInfo(name, false), value, StoreLocation.Raw);
return true;

Work takes place inside the function TryCheckHeaderName().

, RFC HTTP ( ..), .

:

bool TryCheckHeaderName(string name)
{
   if (string.IsNullOrEmpty(name))
   {
       return false;
   }
   if (HttpRuleParser.GetTokenLength(name, 0) != name.Length)
   {
       return false;
   }
   if ((this.invalidHeaders != null) && this.invalidHeaders.Contains(name))
   {
       return false;
   }
   return true;
}

( ), , TryCheckHeaderName.

+8

TryAddWithoutValidation , , , (, "" ). Add ,

+1

All Articles