Jensen
Jensen

Reputation: 1329

HttpWebResponse difference between Headers and GetResponseHeader

When i've got a HttpWebResponse object, there are two ways to access the response headers:

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

They both return the same value, so why there are two ways to obtain header information? Looking at the .NET sources, if found the implementation for both in 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 ); 
    }

Only thing i can see is, that with the Headers property i can enumerate through all availiable headers. Any ideas?

Thanks!

Upvotes: 0

Views: 2488

Answers (1)

jhoanna
jhoanna

Reputation: 1857

According to the MSDN library, the Headers property is a WebHeaderCollection of all the headers. Since it is a collection, it is useful for accessing multiple headers' names, values, or both. It can also access a single header's value by indicating the name in the Header[<name>] format.

GetResponseHeader() on the other hand, is a method that returns the value of a single value only.

In summary, the differences are:

  1. Property vs method
  2. Multiple header name and/or value access vs single header value access

Upvotes: 2

Related Questions