How to read HTTP response headers from web service response?

Guy Bertental picture Guy Bertental · Nov 29, 2009 · Viewed 7.4k times · Source

How can I read the HTTP response headers from a web service response in C#?

Answer

Guy Bertental picture Guy Bertental · Nov 29, 2009

After digging through MSDN, all I needed to do was to override the GetWebResponse method, and then I could access the response headers:

public class MyWSProxy : HttpWebClientProtocol
{
    protected override WebResponse GetWebResponse(WebRequest request)
    {
        System.Net.WebResponse wr = base.GetWebResponse(request);

        // read a response header
        object val = wr.Headers["key"];

        return wr;
    }
}