2009-12-02 118 views
2
WebClient Client = new WebClient(); 
Client.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.BypassCache); 

爲什麼上面的代碼實際上並未阻止通過.Net Web Client緩存站點?C#:無法繞過緩存(WebClient)

+0

你是如何看到自己被緩存?這只是向請求添加一個頭,它不控制服務器實際發送的內容。 – Cine 2010-11-29 07:36:53

回答

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

我試過NoCacheNoStore以及BypassCache,它們都不起作用。 – Paul 2009-12-02 15:17:14

+0

無RequestCacheLevel適用於我。在wpf中使用webclient。 – 2011-02-24 09:08:00