Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 806
Troels Thomsen Avatar asked Feb 10 '09 13:02

Troels Thomsen


People also ask

What is the difference between HttpWebRequest and WebRequest?

WebRequest is an abstract class. The HttpWebRequest class allows you to programmatically make web requests to the HTTP server.

What is HttpWebRequest?

The HttpWebRequest class provides support for the properties and methods defined in WebRequest and for additional properties and methods that enable the user to interact directly with servers using HTTP.


2 Answers

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.

like image 116
AMing Avatar answered Sep 25 '22 14:09

AMing


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);
}
like image 21
Bradley Grainger Avatar answered Sep 25 '22 14:09

Bradley Grainger