简体   繁体   中英

How to clear the cache of HttpWebRequest

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:

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 HttpWebRequest cache? The right solution would be to fix the external source or perhaps change the cache policy, but neither is possible - hence the question.

Clearing the cache could have various impacts, so preferably the solution would be to invalidate the cache on a per resource basis.

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.

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

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.

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

添加以下行以在Webclient获取数据时清除缓存:

Webclient.Headers.Add(HttpRequestHeader.CacheControl, "no-cache")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM