简体   繁体   中英

How to determine if an HttpResponseMessage was fulfilled from cache using HttpClient

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

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

FYI: The Windows.Web.Http HttpClient (a similar API targetted at Windows 8.1 app development) does include an HttpResponseMessage.Source field that specifies where the result came from (common values are "cache" and "network").

The Windows.Web.Http classes are usable from C# and other .NET languages, from C++, and from JavaScript (when running as a WwaHost app like from the Windows app store).

Can I ask what you're trying to achieve? Are trying to avoid caching?

The reason for asking is I've looked at the source code for HttpClient (specifically HttpClientHandler ) and the source for HttpWebResponse and I dont believe you can get this information from the headers.

HttpClient/HttpClientHandler does use HttpWebResponse internally however it does not expose all properties from 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 the way I see it are:

a) Look at the source code for HttpWebRequest to determine the logic for IsFromCache and retrofit this somehow into HttpClient (this may not even be possible, depends on what the logic actually does/needs)

b)ask the ASP.NET team for this property to be included with HttpResponseMessage. either directly as a property or perhaps they could 'keep' the HttpWebResponse

Neither of these options are that great sorry, hence my original question, what are you trying to acheive?

I've been struggling with this scenario recently as well.

What I needed was an integration test to verify that:

  • Responses for a newly created resource had the correct headers set by the server.
  • Subsequent requests for that resource were fulfilled from the client-cache.
  • Responses for an existing resource had the correct headers set by the server as well.

What I ended up doing was a twofold check:

A non-caching HttpClient to check the initial response:

new WebRequestHandler
{
  AllowAutoRedirect = true,
  UseCookies = true,
  CookieContainer = new CookieContainer(),
  CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.Refresh)
};

var client = new HttpClient(handler)

and a second HTTP client to check the client-side cache:

new WebRequestHandler
{
  AllowAutoRedirect = true,
  UseCookies = true,
  CookieContainer = new CookieContainer(),
  CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.Default)
};

var client = new HttpClient(handler)

To verify the source of response messages I compare the HttpResponseMessage.Headers.Date values from steps 1 and 2 (which will be the same if the response came from the client cache). For my third step I can just re-use the client from the first step and append an arbitrary string to the URL.

Disclaimer: this applies to .NET Framework 4.7 and ignores best practices concerning HttpClient usage but is seems to do the trick for me in my test suite. An explicit property like the one mentioned above would be preferable but does not seem to be available. Since the last reply here is already a few years old there might be better ways to handle this, but I couldn't think of one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM