views:

119

answers:

2

I'm using either WebClient or HttpRequest/REsponse to make http calls for images.

I'm not sure how caching specifically works for browsers, but is there a way to enable either WebClient or WebHttpRequest to use the systems "temporary internet files" cache that the browser utilize?

Or, do I have to write my own disk cacher?

A: 

If you're making HTTP requests and you want your requests to access a local cache if the requested resource has already been requested, you'll have to write your own cache.

There are many potential implementations. It really depends on how many different resources you expect to request, and how often.

Chris
Thanks. I just ended up writing my own. Only took 5 minutes to get an API I was happy with
CVertex
@Chris: is there any documentation on this? I'm not convinced this is true.
AnthonyWJones
Nor I, and there's the HttpRuntime.Cache at worst
annakata
@Anthony: what do you mean? The .NET framework is not married to IE. If there is a way, it's part of a the Windows API, or an IE assembly reference.
Chris
No its not but IE doesn't implement the HTTP protocol weither, thats done by WinInet and there are other clients of WinInet. So the question is does WebClient use the WinInet stack (or mimic it) or does it behave entirely independantly. I doubt it does.
AnthonyWJones
I know for a fact that WebClient implementation in Silverlight uses the WinInet stack and its implementation of the cache, whether that is true of standard WebClient I can't be positive. I was wondering if you had access to some info I currently lack?
AnthonyWJones
WebClient does not innately implement anything that will allow it to access IE's cache. There may be a way, but it's not part of the .NET framework (it would be Windows/WinInet API).
Chris
I don't. I just know intimately what the System.Net namespace and System.Net.WebClient class are capable of. Like I said, there may be a way, but he's going to have to look outside of System.Net and WebClient to accomplish this, if he can.
Chris
+2  A: 

You can instruct the WebRequest to use system cache by setting the CachePolicy property.

Following code (taken from MSDN) caches requests for one day. The cache is stored at the temporay internet files folder of the current user (at least on Windows XP).

// Create a policy that allows items in the cache
// to be used if they have been cached one day or less.
HttpRequestCachePolicy requestPolicy = 
  new HttpRequestCachePolicy (HttpCacheAgeControl.MaxAge,
  TimeSpan.FromDays(1));

WebRequest request = WebRequest.Create (resource);

// Set the policy for this request only.
request.CachePolicy = requestPolicy;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

// Determine whether the response was retrieved from the cache.
Console.WriteLine ("The response was retrieved from the cache : {0}.",
 response.IsFromCache);

Stream s = response.GetResponseStream ();
// do something with the response stream
s.Close();
response.Close();
Martin Vobr