System.Net.Http.HttpClient缓存行为 [英] System.Net.Http.HttpClient caching behavior

查看:948
本文介绍了System.Net.Http.HttpClient缓存行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的HttpClient 0.6.0从的NuGet

I'm using HttpClient 0.6.0 from NuGet.

我有以下的C#代码:

var client = new HttpClient(new WebRequestHandler() {
    CachePolicy =
        new HttpRequestCachePolicy(HttpRequestCacheLevel.CacheIfAvailable)
});
client.GetAsync("http://myservice/asdf");



服务(此时CouchDB的)返回一个ETag值和状态码200 OK。还有就是返回一个Cache-Control头与值必须-重新验证

The service (this time CouchDB) returns an ETag value and status code 200 OK. There is returned a Cache-Control header with value must-revalidate

更​​新,这里的响应头从CouchDB的(从Visual Studio调试器拍摄):

Update, here are the response headers from couchdb (taken from the visual studio debugger):

Server: CouchDB/1.1.1 (Erlang OTP/R14B04)
Etag: "1-27964df653cea4316d0acbab10fd9c04"
Date: Fri, 09 Dec 2011 11:56:07 GMT
Cache-Control: must-revalidate

下一次,我做同样的要求,HttpClient的做了有条件的请求,回来304未修改。这是正确的。

Next time I do the exact same request, HttpClient does a conditional request and gets back 304 Not Modified. Which is right.

不过,如果我使用低级别的HttpWebRequest类具有相同的CachePolicy,请求甚至没有做第二次。这就是我想要的HttpClient还的行为方式。

However, if I am using low-level HttpWebRequest class with the same CachePolicy, the request isn't even made the second time. This is the way I would want HttpClient also behave.

这是不是必重新验证头值或者为什么HttpClient的表现不同?我想这样做只有一个要求,然后有从缓存中剩下的没有条件请求。

Is it the must-revalidate header value or why is HttpClient behaving differently? I would like to do only one request and then have the rest from cache without the conditional request..

(此外,作为一个侧面说明,在调试时,响应状态代码显示为200 OK,即使该服务将返回304未修改)

(Also, as a side-note, when debugging, the Response status code is shown as 200 OK, even though the service returns 304 Not Modified)

推荐答案

这两个客户端的行为正确。

Both clients behave correctly.

必重新验证仅适用于的陈旧的反应的。

must-revalidate only applies to stale responses.

当必revalidate指令存在于一个缓存收到响应,它变得陈旧之后缓存绝不能使用输入的 的回应到
后续请求,而不先用重新验证它原始服务器。 (即缓存必须每次做一个终端到终端的重新验证,的如果的,完全基于原始服务器的过期或max-age的值,缓存响应是陈旧

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.)

由于您没有提供明确的到期,的caches被允许使用启发式,以确定新鲜感

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

既然你不提供的Last-Modified 不需要警告这是使用启发式客户

如果没有的过期,缓存控制:最大年龄,或缓存控制:S-超过maxage(见14.9.3)出现在响应,响应不包括缓存等限制,的缓存可以计算使用启发式的新鲜感寿命。该缓存必须贴上警告113到任何回应的,其年龄超过24小时的,如果尚未添加这样的警告。

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.

响应年龄是基于计算的日期因为年龄不存在。

如果响应仍然根据启发式过期新鲜,高速缓存可以使用存储的响应。

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

一个解释是,的HttpWebRequest 使用启发式和有一个与状态代码200,这是记忆犹新存储的响应。

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

这篇关于System.Net.Http.HttpClient缓存行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆