2012-06-11 28 views
0

之間的區別。當我有一個HttpWebResponse對象,有訪問響應頭有兩種方式:HttpWebResponse頁眉和GetResponseHeader

string dateHeader = webResponse.Headers["Date"]; 
string dateHeader = webResponse.GetResponseHeader("Date"); 

他們都返回相同的值,那麼,爲什麼有兩種方式獲得頭信息? 望着.NET人士透露,如果發現執行情況無論是在HttpWebReponse:

// retreives response header object 
    /// <devdoc> 
    /// <para> 
    ///  Gets 
    ///  the headers associated with this response from the server. 
    /// </para> 
    /// </devdoc> 
    public override WebHeaderCollection Headers { 
     get { 
      CheckDisposed(); 
      return m_HttpResponseHeaders; 
     } 
    } 

    /// <devdoc> 
    /// <para> 
    ///  Gets a specified header value returned with the response. 
    /// </para> 
    /// </devdoc> 
    public string GetResponseHeader(string headerName) { 
     CheckDisposed(); 

     string headerValue = m_HttpResponseHeaders[headerName]; 

     return ((headerValue==null) ? String.Empty : headerValue); 
    } 

我唯一能看到的是,與該標題屬性,我可以通過所有availiable頭枚舉。有任何想法嗎?

謝謝!

回答

2

根據MSDN libraryHeaders屬性是所有標題的WebHeaderCollection。由於它是一個集合,因此訪問多個頭的名稱,值或兩者都很有用。它還可以通過以Header[<name>]格式指示名稱來訪問單個標題的值。

GetResponseHeader()另一方面,是一種僅返回單個值的值的方法。

綜上所述,區別是:

  1. 屬性VS方法
  2. 多個標題名稱和/或值訪問VS單頭值訪問