I'm using monotouch for creating a iphone applicant which called a unsure page which returns JSON.
Strange one this... but when I use HttpWebRequest the output I receive is cached from an earlier request.
I timed it using a stop watch and I have to wait two minutes before I get a new version of the page up until this point every time I call HttpWebRequest I get output thats been cached.
Even if I kill the app and reload it the output is still the same for that two minute window.
开发者_如何学运维This happens on both the simulator and the phone.
I've just tried adding a cache policy and no joy - I'm still getting all responses cached for 2 minutes.
heres my latests code:
Uri address = new Uri(url);
HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
System.Net.Cache.RequestCachePolicy c = new System.Net.Cache.RequestCachePolicy();
c.Level = RequestCacheLevel.NoCacheNoStore;
request.CachePolicy = c;
request.Timeout = Settings.RequestTimeout;
HttpWebResponse response = request.GetResponse();
Any one have any ideas?
Just an FYI, but Mono's HttpWebRequest implementation doesn't do any caching (yet). It may be implemented in the future, but as of 2.10.3 it doesn't.
Have you tried the request to a different address? Could it be caching the response server side for two minutes?
You could try using the iOS related objects and methods. Seeing the caching is definitely implemented when using NSUrlCache in MonoTouch, and you can bypass it with the right NSUrlRequestCachePolicy enum. Here is how I did it when I wanted to avoid hitting the cache:
NSUrl url = new NSUrl (url);
NSUrlRequest request = new NSUrlRequest (url,
NSUrlRequestCachePolicy.ReloadIgnoringCacheData,
0.0);
//set a delegate to handle the request callback, subclass this yourself
_webView.Delegate = new UIWebViewDelegate();
_webView.LoadRequest (request);
Hope this helps.
精彩评论