Daniel Clark
Daniel Clark

Reputation: 615

WebRequest Caching Windows Phone 7

From what I understand, the HttpWebRequest class always cache the downloaded data. Now I don't mind this, but after a throughly reparsing the same URL through HttpWebRequest during the app duration, I've noticed that the data becomes corrupted (as in the downloaded JSON data becomes unparsable). After rebooting the Phone Emulator, it all goes smoonthy until it happens again.

Now I am just wondering if it possible to turn off the caching in HttpWebRequest.

Here is some of the code I am using to make a httpwebrequest call:

var request = (HttpWebRequest)WebRequest.Create(string.Format(uri));

        request.BeginGetResponse(a =>
        {
            var response = request.EndGetResponse(a);
            var responseStream = response.GetResponseStream();
            using (var sr = new StreamReader(responseStream))
            {
                string json = sr.ReadToEnd();
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    //Newtonsoft.Json.Linq;
                    JObject artistObject = JObject.Parse(json);
                    //...etc
                });
            }
        }, null);
    }

Upvotes: 2

Views: 1579

Answers (1)

Mick N
Mick N

Reputation: 14882

A common technique to get around this caching is to add an parameter to the query string that is incremented on successive calls. This thread discusses the silverlight behaviour in more detail, and covers some server handling you can look at too if you have that control.

WebClient Caching Problem

With that said, have you been able to produce a simple repro of the corruption you're experiencing? It might be worth getting that looked into.

Upvotes: 4

Related Questions