问题描述
WebClient.DownloadStringAsync 会缓存服务器响应.一次从服务器得到响应后,即使没有互联网连接,我也会得到响应!
WebClient.DownloadStringAsync does cache the server response.After once getting a response from the server I get a response even without internet connection!
WebClient 缓存是否足够智能,可以根据服务器响应确定缓存多长时间?或者它有问题,我应该禁用缓存.
Is WebClient caching smart enough to determine from the server response how long to cache?Or is it buggy and I should disable caching.
背景信息:
网址:http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml
提琴手跟踪:
GET/stats/eurofxref/eurofxref-daily.xml HTTP/1.1接受:/参考:file:///Applications/Install/4D0DF1F7-1481-45CA-86BE-C14FF5CCD955/Install/接受编码:身份用户代理:NativeHost主持人:www.ecb.europa.eu连接:Keep-Alive
GET /stats/eurofxref/eurofxref-daily.xml HTTP/1.1Accept: /Referer: file:///Applications/Install/4D0DF1F7-1481-45CA-86BE-C14FF5CCD955/Install/Accept-Encoding: identityUser-Agent: NativeHostHost: www.ecb.europa.euConnection: Keep-Alive
HTTP/1.1 200 正常日期:2012 年 3 月 25 日星期日 08:54:40 GMT服务器:Apache/2.2.3 (Linux/SUSE)上次修改时间:2012 年 3 月 23 日星期五 13:31:39 GMTETag:19d4e5-6a9-4bbe90b5904c0"接受范围:字节内容长度:1705保持活动:超时=3,最大值=200连接:保持活动内容类型:文本/xml设置-Cookie:BIGipServerPOOL.www.ecb.europa.eu_HTTP=2684883628.16415.0000;路径=/...
HTTP/1.1 200 OKDate: Sun, 25 Mar 2012 08:54:40 GMTServer: Apache/2.2.3 (Linux/SUSE)Last-Modified: Fri, 23 Mar 2012 13:31:39 GMTETag: "19d4e5-6a9-4bbe90b5904c0"Accept-Ranges: bytesContent-Length: 1705Keep-Alive: timeout=3, max=200Connection: Keep-AliveContent-Type: text/xmlSet-Cookie: BIGipServerPOOL.www.ecb.europa.eu_HTTP=2684883628.16415.0000; path=/...
通过标题禁用缓存不起作用:
Disabling caching via Headers does not work:
.Headers("cache-control") = "no-cache".Headers("HttpRequestHeader.IfModifiedSince") = DateTime.UtcNow.ToString()
.Headers("cache-control") = "no-cache".Headers("HttpRequestHeader.IfModifiedSince") = DateTime.UtcNow.ToString()
通过附加 uniqa 参数禁用缓存有效:
Disabling caching via appending uniqa parameter works:
http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"&"?MakeRequestUnique=" &环境.TickCount
"http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml" & "?MakeRequestUnique=" & Environment.TickCount
推荐答案
集成缓存一点也不智能.因此,如果您在查询页面时期望得到不同的结果,则必须绕过它.我说绕过"是因为我不知道用 WebClient 禁用它(如果你直接使用 HttpRequest 类,我认为它不会被启用).所以如果你想使用WebClient,最好的方法是在请求中附加一个随机参数.
The integrated cache isn't smart at all. So if you expect different results when querying the page, you have to bypass it. I say 'bypass' because there's no way I know of to disable it with the WebClient (I don't think it's enabled if you directly use the HttpRequest class).So if you want to use the WebClient, the best way is to append a random parameter to the request.
这篇关于我应该禁用 WebClient 缓存吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!