How to clear the cache of HttpWebRequest

前端 未结 5 1995
说谎
说谎 2020-11-29 09:05

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 th

相关标签:
5条回答
  • 2020-11-29 09:30

    HttpWebRequest uses System.Net.Cache.RequestCache for caching. This is an abstract class; the actual implementation in the Microsoft CLR is Microsoft.Win32.WinInetCache which, as the name implies, uses the WinInet functions for caching.

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

    Assuming that clearing the WinInet cache solves the problem, you can delete files programmatically by P/Invoking to the DeleteUrlCacheEntry WinInet API:

    public static class NativeMethods
    {
        [DllImport("WinInet.dll", PreserveSig = true, SetLastError = true)]
        public static extern void DeleteUrlCacheEntry(string url);
    }
    
    0 讨论(0)
  • 2020-11-29 09:34
    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.

    0 讨论(0)
  • 2020-11-29 09:37

    I haven't tried, but a solution could be to add an arbitrary query string to the url being requested.

    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.

    0 讨论(0)
  • 2020-11-29 09:38

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

    0 讨论(0)
  • 2020-11-29 09:43

    Add the following line to clear the cache as and when the Webclient fetches the data:

    Webclient.Headers.Add(HttpRequestHeader.CacheControl, "no-cache")
    
    0 讨论(0)
提交回复
热议问题