C# Webclient Disable Cache

C# WebClient disable cache

From the above I would guess that you have problem somewhere else. Can you log http requests on server side? What do you get when you alter some random seed parameter?

Maybe SERVER caches the file (if the log shows that request is really triggered every minute.

Do you use ISA or SQUID?

What is http response code for your request?

I know that answering with answers might not be popular, but comment doesn't allow me this much text :)

EDIT:

Anyway, use HttpRequest object instead of WebClient, and hopefully (if you place your doubts in WebClient) everything will be solved. If it wasn't solved with HttpRequest, then the problem really IS somewhere else.

Further refinement:

Go even lower: How do I Create an HTTP Request Manually in .Net?

This is pure sockets, and if the problem still persists, then open a new question and tag it WTF :)

WebClient().DownloadString() returning old data

Try disabling the cache on the WebClient

webClient.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);

MSDN Documentation on WebClient Cache

Timer or webclient in c# somehow keeping hold on to the previous variable values

see C# WebClient disable cache


see https://msdn.microsoft.com/en-us/library/system.net.webclient.cachepolicy(v=vs.110).aspx


in short WebClient can cache. The first question details the difficulty in trying to prevent it caching

Should I disable WebClient caching?

The integrated cache isn't smart at all. So if you expect different results when querying the page, you have to bypass it. I say 'bypass' because there's no way I know of to disable it with the WebClient (I don't think it's enabled if you directly use the HttpRequest class).
So if you want to use the WebClient, the best way is to append a random parameter to the request.

Is there any way to force WebClient, not return cached data?

This is a tricky one as the caching is usually being caused by the website's headers not specifying a no-cache. I've found that in the past the easiest way to deal with these caching issues is simply to provide a randomised query string parameters so that the web server interprets each request as a fresh request.

if you're currently requesting www.domain.com/image.jpg then try www.domain.com/image.jpg?rand=XXXX where XXXX is a random value generated in your server side code.

Must I reset the webclient?

This may be a caching issue. Check your response object to see if the IsFromCache property is true. If it is, there are a few different ways to deal with it.

Add cache control headers on the server side as described in Example #2 of the header article on the PHP site:

Example #2 Caching directives

PHP scripts often generate dynamic content that must not be cached by
the client browser or any proxy caches between the server and the
client browser. Many proxies and clients can be forced to disable
caching with:

<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
?>

Note:

You may find that your pages aren't cached even if you don't output
all of the headers above. There are a number of options that users may
be able to set for their browser that change its default caching
behavior. By sending the headers above, you should override any
settings that may otherwise cause the output of your script to be
cached.

Additionally, session_cache_limiter() and the session.cache_limiter
configuration setting can be used to automatically generate the
correct caching-related headers when sessions are being used.

Change the caching policy for your application domain on the client:

// Set a default policy level for the "http:" and "https" schemes.
HttpRequestCachePolicy policy = new HttpRequestCachePolicy(HttpRequestCacheLevel.Default);
HttpWebRequest.DefaultCachePolicy = policy;

Change the caching policy for the web request on the client:

WebRequest request = WebRequest.Create(uri);
// Define a cache policy for this request only.
HttpRequestCachePolicy noCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
request.CachePolicy = noCachePolicy;

See the MSDN documentation for HttpRequestCacheLevel Enumeration for more information on the client side methods.



Related Topics



Leave a reply



Submit