System.Net.Http.HttpClient caching behavior

后端 未结 2 1881
余生分开走
余生分开走 2020-12-18 19:06

I\'m using HttpClient 0.6.0 from NuGet.

I have the following C# code:

var client = new HttpClient(new WebRequestHandler() {
    CachePolicy =
                


        
相关标签:
2条回答
  • 2020-12-18 19:36

    Answering my own question..

    According to http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.4 I would say that a "Cache-Control: must-revalidate" without expiration states that the resource should be validated on every request.

    In this case it means a conditional GET should be done every time the resource is made. So in this case System.Net.Http.HttpClient is behaving correctly and the legacy (Http)WebRequest is doing invalid behavior.

    0 讨论(0)
  • 2020-12-18 19:48

    Both clients behave correctly.

    must-revalidate only applies to stale responses.

    When the must-revalidate directive is present in a response received by a cache, that cache MUST NOT use the entry after it becomes stale to respond to a subsequent request without first revalidating it with the origin server. (I.e., the cache MUST do an end-to-end revalidation every time, if, based solely on the origin server's Expires or max-age value, the cached response is stale.)

    Since you do not provide explicit expiration, caches are allowed to use heuristics to determine freshness.

    Since you do not provide Last-Modified caches do not need to warn the client that heuristics was used.

    If none of Expires, Cache-Control: max-age, or Cache-Control: s- maxage (see section 14.9.3) appears in the response, and the response does not include other restrictions on caching, the cache MAY compute a freshness lifetime using a heuristic. The cache MUST attach Warning 113 to any response whose age is more than 24 hours if such warning has not already been added.

    The response age is calculated based on Date header since Age is not present.

    If the response is still fresh according to heuristic expiration, caches may use the stored response.

    One explanation is that HttpWebRequest uses heuristics and that there was a stored response with status code 200 that was still fresh.

    0 讨论(0)
提交回复
热议问题