How to determine if HttpResponseMessage is done from cache using HttpClient

What is equivalent to WebResponse.IsFromCache when using HttpClient and HttpResponseMessage ?

Is there any HTTP header in the response that I can look at?

+6
source share
2 answers

FYI: Windows.Web.Http HttpClient (a similar API for developing Windows 8.1 applications) includes the HttpResponseMessage.Source field, which indicates where the result came from (common values ​​are β€œcache” and β€œnetwork”).

Windows.Web.Http classes can be used in C # and other .NET languages, starting with C ++ and JavaScript (when working as a WwaHost application, for example, from the Windows application store).

+2
source

May I ask what are you trying to achieve? Trying to Avoid Caching?

Reason for the request: I looked at the source code for HttpClient (specifically HttpClientHandler ) and the source for HttpWebResponse , and I do not believe that you can get this information from the headers.

The HttpClient / HttpClientHandler uses the HttpWebResponse internally, but does not disclose all the properties from the HttpWebResponse :

 private HttpResponseMessage CreateResponseMessage(HttpWebResponse webResponse, HttpRequestMessage request) { HttpResponseMessage httpResponseMessage = new HttpResponseMessage(webResponse.StatusCode); httpResponseMessage.ReasonPhrase = webResponse.StatusDescription; httpResponseMessage.Version = webResponse.ProtocolVersion; httpResponseMessage.RequestMessage = request; httpResponseMessage.Content = (HttpContent) new StreamContent((Stream) new HttpClientHandler.WebExceptionWrapperStream(webResponse.GetResponseStream())); //this line doesnt exist, would be nice httpResponseMessage.IsFromCache = webResponse.IsFromCache;// <-- MISSING! ... } 

So, your options, as I see it, are:

a) Look at the source code for HttpWebRequest to determine the logic for IsFromCache and somehow modify it in HttpClient (it may even be impossible, it depends on what the logic actually does / needs)

b) ask the ASP.NET team to include this property in the HttpResponseMessage. either directly as a property, or perhaps they can "save" HttpWebResponse

None of these options are too bad, so my original question is, what are you trying to achieve?

+1
source

All Articles