How to force HttpWebRequest to use cache in ASP.NET environment?
- by piotrsz
In my ASP.NET app I use HttpWebRequest for fetching external resources which I'd like to be cached. Consider the following code:
var req = WebRequest.Create("http://google.com/");
req.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.CacheIfAvailable);
var resp = req.GetResponse();
Console.WriteLine(resp.IsFromCache);
var answ = (new StreamReader(resp.GetResponseStream())).ReadToEnd();
Console.WriteLine(answ.Length);
HttpWebRequest uses IE cache, so when I run it as normal user, data is cached to %userprofile%\Local Settings\Temporary Internet Files and next responses are read from cache.
I thought that when such code is run inside ASP.NET app, data will be cached to ...\ASPNET\Local Settings\Temporary Internet Files but it is not and cache is never used.
What I am doing wrong? How to force HttpWebRequest to use cache in ASP.NET environment?