Over a million developers have joined DZone.

Creating a Windows Phone App: caching urls problem

DZone 's Guide to

Creating a Windows Phone App: caching urls problem

· Mobile Zone ·
Free Resource

After two weeks of programming I can finally state that my first Windows Phone 7 application is 100% functional and ready. Now it only needs to go through the marketplace certification tests and then I’ll be the proud owner of an official WP7 app.The following days I will put up short blogs describing some of the things I’ve learned while making this app; small tips, links to interesting guides, etc. I will not describe the app itself in these posts (scheduled once the app is published) and try to formulate my problems and solutions as generic as possible. Consider these posts a gathering of my recent acquired new WP7 development knowledge.

This first post will explain the problem with WP7 devices caching urls you are trying to reach.

Sending commands to my mediacenter (the app I made is an IP remote control for my media center) is done by sending an http-request of the following form (shortened):

uristring = http://mymediacenter/remoteservice.php?fcm=c

This request will send the c-command to the remote. Implementing this in WP7 is a breeze using the WebClient class:

WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(sendCommandCompleted);

where uri contains the httprequest string containing the command. The moment DownloadStringAsync() is started and once the request arrives at the media center, the command is executed. The media center service will return a “success” string which can be parsed in the sendCommandCompleted method for further error checking if needed.

Actually this code is heart of my app and should work always….however, it doesn’t on my WP7 phone. What happens is this: the WebClient-class will cache any url that was previously send on the device. If at any time during the lifetime of the app the same url needs to be send again, the device will simply NOT send the request and return the original result it received from the request to the sendCommandCompleted method. In other words, to the user it will look like the request was in fact send, however, nothing is transmitted ‘over the wire’. The only workaround without changing your code is by restarting the application on your phone. This will empty the cache, but once again you can only send each command once.

The only way to solve this particular problem is by creating a new url each time, making the WP7 device believe that is has never before send anything to the given url (other solutions exist if you have access to the service you are using, see here). The best way to do this is to add a random string as extra parameter behind the original url, for example:

uristring += "&r=" + Guid.NewGuid().ToString();

This will result in the following string, where the part after r= will vary each time:


Two notes here:

  1. The ‘r’ parameter can be any letter you choose, as long as it’s not a parameter your remote services knows (otherwise it will try and parse the random string right behind it)
  2. Using Guid, a rather long random string is created (16bytes). Depending on how long the app will typically live it might be interesting to have a shorter random string (simply use the Random class) in order to conserve precious bits being send on your phone.

Problem 1 solved!

Source: https://timdams.wordpress.com/2011/06/17/creating-a-wp-7-app-caching-urls/


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}