Peter Meinl
Peter Meinl

Reputation: 2586

Should I disable WebClient caching?

WebClient.DownloadStringAsync does cache the server response. After once getting a response from the server I get a response even without internet connection!

Is WebClient caching smart enough to determine from the server response how long to cache? Or is it buggy and I should disable caching.

Backgound info:

Url: http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml

Fiddler trace:

GET /stats/eurofxref/eurofxref-daily.xml HTTP/1.1 Accept: / Referer: file:///Applications/Install/4D0DF1F7-1481-45CA-86BE-C14FF5CCD955/Install/ Accept-Encoding: identity User-Agent: NativeHost Host: www.ecb.europa.eu Connection: Keep-Alive

HTTP/1.1 200 OK Date: Sun, 25 Mar 2012 08:54:40 GMT Server: Apache/2.2.3 (Linux/SUSE) Last-Modified: Fri, 23 Mar 2012 13:31:39 GMT ETag: "19d4e5-6a9-4bbe90b5904c0" Accept-Ranges: bytes Content-Length: 1705 Keep-Alive: timeout=3, max=200 Connection: Keep-Alive Content-Type: text/xml Set-Cookie: BIGipServerPOOL.www.ecb.europa.eu_HTTP=2684883628.16415.0000; path=/ ...

Disabling caching via Headers does not work:

.Headers("cache-control") = "no-cache" .Headers("HttpRequestHeader.IfModifiedSince") = DateTime.UtcNow.ToString()

Disabling caching via appending uniqa parameter works:

"http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml" & "?MakeRequestUnique=" & Environment.TickCount

Upvotes: 1

Views: 1848

Answers (1)

Kevin Gosse
Kevin Gosse

Reputation: 39007

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.

Upvotes: 2

Related Questions