Are there any constants for the default HTTP headers?

后端 未结 8 909
时光说笑
时光说笑 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-01 01:20

    To expand on Jed's answer.

    The HttpResponseHeader and HttpRequestHeader enumerations can be used as your constants when using the WebHeaderCollection. WebHeaderCollection contains indexer properties that accept these enumerations.

    You can use either a string or one of the enumerations to get and set the header value, and mix it up as well within you code.

    Example LinqPad script:

    var headers = new WebHeaderCollection();
    
    headers["If-Modified-Since"] = "Sat, 29 Oct 1994 19:43:31 GMT"; 
    // shows header name as "If-Modified-Since"
    headers.Dump();
    // shows expected header value of "Sat, 29 Oct 1994 19:43:31 GMT"
    headers[HttpRequestHeader.IfModifiedSince].Dump();
    
    headers.Clear();
    
    headers[HttpRequestHeader.IfModifiedSince] = "Sat, 29 Oct 1994 19:43:31 GMT";
    // shows header name as "If-Modified-Since"
    headers.Dump(); 
    // shows expected header value "Sat, 29 Oct 1994 19:43:31 GMT"
    headers["If-Modified-Since"].Dump();
    

提交回复
热议问题