How to force HttpWebRequest to use cache in ASP.NET environment?

Posted by piotrsz on Stack Overflow See other posts from Stack Overflow or by piotrsz
Published on 2010-05-13T11:59:58Z Indexed on 2010/05/13 12:04 UTC
Read the original article Hit count: 250

Filed under:
|
|
|

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?

© Stack Overflow or respective owner

Related posts about httpwebrequest

Related posts about cache