Are there any constants for the default HTTP headers?

后端 未结 8 845
时光说笑
时光说笑 2021-02-01 00:21

Has Microsoft created a class full of constants for the standard HTTP header names or will I have to write my own?

8条回答
  •  故里飘歌
    2021-02-01 01:01

    using System.Net.HttpRequestHeader;
    using System.Net.HttpResponseHeader;
    
    public class Example {
      static void Main() {
        Console.WriteLine(HttpRequestHeader.IfModifiedSince.ToHeaderString());
        // If-Modified-Since
    
        Console.WriteLine(HttpResponseHeader.ContentLength.ToHeaderString());
        // Content-Length
      }
    }
    
    static class ExtensionMethods {
      public static string ToHeaderString(this HttpRequestHeader instance)
      {
        return Regex.Replace(instance.ToString(), "(\\B[A-Z])", "-$1");
      }
    
      public static string ToHeaderString(this HttpResponseHeader instance)
      {
        return Regex.Replace(instance.ToString(), "(\\B[A-Z])", "-$1");
      }
    }
    

提交回复
热议问题