Monotouch - iphone HttpWebRequest 关闭缓存?
我正在使用 monotouch 创建一个 iphone 应用程序,该应用程序调用一个返回 JSON 的不确定页面。
这很奇怪......但是当我使用 HttpWebRequest 时,我收到的输出是从早期请求缓存的。
我使用秒表对其进行计时,并且必须等待两分钟才能获得页面的新版本,直到此时每次调用 HttpWebRequest 时我都会获得已缓存的输出。
即使我终止应用程序并重新加载它,两分钟窗口的输出仍然相同。
这种情况在模拟器和手机上都会发生。
我刚刚尝试添加缓存策略,但没有高兴 - 我仍然将所有响应缓存 2 分钟。
这是我的最新代码:
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();
有人有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
仅供参考,但 Mono 的 HttpWebRequest 实现尚未执行任何缓存。将来可能会实现,但从 2.10.3 开始还没有实现。
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?
您可以尝试使用iOS相关的对象和方法。看到在 MonoTouch 中使用 NSUrlCache 时肯定会实现缓存,并且您可以使用正确的 NSUrlRequestCachePolicy 枚举来绕过它。当我想避免命中缓存时,我是这样做的:
希望这会有所帮助。
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:
Hope this helps.