4

I use RestSharp in my Windows Phone 7.1 project.

My problem is RestSharp always cache response data.

Example:

At the first time I send request, it returns data correctly. After some delete operations, I send that request again, but response seems the same as the first time, nothing's changed.

If I stop debugging and press F5 to start again, it works perfectly as expected.

I also tried request.AddParameter("cache-control", "no-cache", ParameterType.HttpHeader); and got no luck.

How can I fix this problem?

Zin
  • 408
  • 5
  • 16
  • 1
    RestSharp doesn't have any built-in caching so this is probably in the HttpWebRequest layer it's built on. Do you have control over the API endpoint? Can you post more of your code? – John Sheehan Apr 19 '12 at 17:29
  • 2
    its a hack but try something like `url = originalUrl + "&nocache=" + DateTime.Now.Ticks` – Rico Suter Apr 19 '12 at 17:31
  • I used a similar solution that @RicoSuter mentions to solve an identical problem. – earthling Apr 20 '12 at 01:54

4 Answers4

4

I have the same issue so just add header that specify not to cache response data client is my RestClient with base url and than add default header Cache-Control with value no-cache.

client.AddDefaultHeader("Cache-Control", "no-cache")
3

I found solution in Rico Suter comment, thanks! I will mark this as accepted anwser

its a hack but try something like url = originalUrl + "&nocache=" + DateTime.Now.Ticks

Zin
  • 408
  • 5
  • 16
1

The "Cache-Control" header should do the trick!

I think HTTP Headers are case-insensitive, but the server may not agree with me there! You should try using Cache-Control instead of cache-control...

Also, I would also add the Pragma header with no-cache value to the request (some old servers don't use the "Cache-Control" header, but they will sure recognize this one)!

And I would try to use Fiddler to debug the comms and check that the headers are really being sent to the server as expected!

Pedro Lamas
  • 7,185
  • 4
  • 27
  • 35
0

Another solution can be to set the "If-Modified-Since" header with value of DateTime.Now:

client.AddDefaultParameter("If-Modified-Since", DateTime.Now, ParameterType.HttpHeader);
EvZ
  • 11,889
  • 4
  • 38
  • 76