Disable REST WebRequest URL caching on WP7 Mango

by

I’m writing an application for Windows Phone 7 Mango that should be hitting the marketplace shortly.  The application allows you to access the real-time feeds that report MBTA bus locations and predictions for when buses will arrive at a particular stop.  These feeds are exposed as REST services. 

Yesterday, I found that I wasn’t able to refresh bus stop predictions and, after I drilled into the problem, I discovered that Mango was caching the result of the request.  Same request URL always returned the same response data.  Well, this wasn’t helpful because the data changes from minute to minute.

Problem: Windows Phone 7.5 (Mango) returns the same HTTP response result for a given request URL sent to a REST web service. 

Cause: The HttpWebRequest and HttpWebResponse objects that are returned by WebRequest.Create(string url) use caching that is based on the URL text.  If the text of the URL doesn’t change (same base URL plus query string parameters), the caching system assumes it can save time and return the same data.

Solution / Workaround: Append the current DateTime.Now.Ticks value to the WebRequest query string.  This makes the web requests reasonably unique from request to request and the responses become un-cacheable.

SNAGHTML320fdee

I hope this helps.

-Ben


4 Responses to "Disable REST WebRequest URL caching on WP7 Mango"
  1. Well, some extremely dumb people at Microsoft made a very bad decision here. My phone app should control and have its own cache. I suppose at Microsoft and you request data from a web page or web service they expect the data to never change. By the way I have worked for Microsoft. I can’t believe this crap!

  2. I had exactly the same problem writing a transit app. However my transit provider rejects the URL if I add an extra query string, so I am unable to use this solution. What a pain.

  3. My problem is similar to that of Mike’s. Never thought Microsoft will make such dumb decisions. These are some of the reasons why Microsoft is an evergreen crap!. I think they thought of saving battery life by such restrictions. Already wasted three days on caching issue!! Still not resolved! Being a mainstream Android developer i am truly frustrated developing apps for WP8….Android is a heaven. This is my last app for WP8. Let windows phone store apps rot in hell!! Good Bye!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.