How to read HTTP response headers from web service

2019-07-18 13:27发布

问题:

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

回答1:

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;
    }
}


回答2:

If you're getting back an HttpResponse, you can just query the HttpResponse.Headers property.



回答3:

Can't you just refer to HttpContext.Current.Response.Headers in your webservice?
I'm not sure if that'll work.