Skip to main content
 首页 » 编程设计

C#:无法绕过缓存 (WebClient)

2024年01月13日28lonelyxmas

WebClient Client = new WebClient(); 
Client.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.BypassCache); 

为什么上面的代码实际上没有阻止网站通过 .Net Web 客户端缓存?

请您参考如下方法:

string xmlUrl = "http://myserver.com/xmlfile.xml"; 
 
WebClient client = new WebClient(); 
 
// prevent file caching by windows 
client.CachePolicy = new System.Net.Cache.RequestCachePolicy( 
System.Net.Cache.RequestCacheLevel.NoCacheNoStore 
); 
 
// read content of file 
Stream rssStream = client.OpenRead(xmlUrl); 

不使用缓存不存储。

编辑:如果它不起作用,请尝试使用 webrequest/webresponse:

 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();