.net 如何清除HttpWebRequest的缓存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/532211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to clear the cache of HttpWebRequest
提问by Troels Thomsen
I am developing against a proprietary library and I'm experiencing some issues with the cache of the HttpWebRequest. The library is using code equivalent to the one below to make the requests:
我正在针对专有库进行开发,并且遇到了HttpWebRequest. 该库正在使用与以下代码等效的代码来发出请求:
var request = WebRequest.Create("http://example.com/") as HttpWebRequest;
request.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.CacheIfAvailable);
The external resource doesn't disallow caching although each response differs. Thus I am ending up getting the same response each time.
尽管每个响应都不同,但外部资源并不禁止缓存。因此,我最终每次都得到相同的响应。
Is there any way to clear the contents of the HttpWebRequestcache? The right solution would be to fix the external source or perhaps change the cache policy, but neither is possible - hence the question.
有什么办法可以清除HttpWebRequest缓存的内容吗?正确的解决方案是修复外部源或可能更改缓存策略,但两者都不可能 - 因此存在问题。
Clearing the cache could have various impacts, so preferably the solution would be to invalidate the cache on a per resource basis.
清除缓存可能会产生各种影响,因此最好的解决方案是在每个资源的基础上使缓存无效。
回答by AMing
public static WebResponse GetResponseNoCache(Uri uri)
{
// Set a default policy level for the "http:" and "https" schemes.
HttpRequestCachePolicy policy = new HttpRequestCachePolicy(HttpRequestCacheLevel.Default);
HttpWebRequest.DefaultCachePolicy = policy;
// Create the request.
WebRequest request = WebRequest.Create(uri);
// Define a cache policy for this request only.
HttpRequestCachePolicy noCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
request.CachePolicy = noCachePolicy;
WebResponse response = request.GetResponse();
Console.WriteLine("IsFromCache? {0}", response.IsFromCache);
return response;
}
You can set the Cache Policy to the request to NoCacheNoStore to the HttpWebRequest.
您可以将缓存策略设置为 NoCacheNoStore 到 HttpWebRequest 的请求。
回答by Bradley Grainger
HttpWebRequestuses System.Net.Cache.RequestCachefor caching. This is an abstract class; the actual implementation in the Microsoft CLR is Microsoft.Win32.WinInetCachewhich, as the name implies, uses the WinInetfunctions for caching.
HttpWebRequest使用System.Net.Cache.RequestCache缓存。这是一个抽象类;Microsoft.Win32.WinInetCache顾名思义,Microsoft CLR 中的实际实现是使用WinInet函数进行缓存。
This is the same cache used by Internet Explorer, so you can manually clear the cache by using IE's Delete Browsing History dialog. (Do this first as a test, to make sure clearing the WinInet cache solves your problem.)
这与 Internet Explorer 使用的缓存相同,因此您可以使用 IE 的“删除浏览历史记录”对话框手动清除缓存。(首先作为测试执行此操作,以确保清除 WinInet 缓存可以解决您的问题。)
Assuming that clearing the WinInet cache solves the problem, you can delete files programmatically by P/Invoking to the DeleteUrlCacheEntryWinInet API:
假设清除 WinInet 缓存解决了问题,您可以通过 P/Invoking 到DeleteUrlCacheEntryWinInet API以编程方式删除文件:
public static class NativeMethods
{
[DllImport("WinInet.dll", PreserveSig = true, SetLastError = true)]
public static extern void DeleteUrlCacheEntry(string url);
}
回答by jussinen
I haven't tried, but a solution could be to add an arbitrary query string to the url being requested.
我还没有尝试过,但解决方案可能是向所请求的 url 添加任意查询字符串。
This querystring would change everytime, maybe using DateTime.Now meaning the url would be different each time. Each request would then get requested supposedly anew.
这个查询字符串每次都会改变,可能使用 DateTime.Now 意味着 url 每次都会不同。然后每个请求都会被重新请求。
回答by Ron Klein
You can change the cache policy: use an http reverse proxy, and remove/change the relevant http headers. It's a hack, but it would work, and quite easily. I'd suggest you use Apache httpd server for this task (with mod_proxy).
您可以更改缓存策略:使用 http 反向代理,并删除/更改相关的 http 标头。这是一个黑客,但它会起作用,而且很容易。我建议你使用 Apache httpd 服务器来完成这个任务(使用 mod_proxy)。
回答by Satish Chandra
Add the following line to clear the cache as and when the Webclient fetches the data:
添加以下行以在 Web 客户端获取数据时清除缓存:
Webclient.Headers.Add(HttpRequestHeader.CacheControl, "no-cache")

