Monotouch - iphone HttpWebRequest 关闭缓存?

发布于 2024-10-17 07:41:36 字数 735 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寂寞笑我太脆弱 2024-10-24 07:41:36

仅供参考,但 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.

捶死心动 2024-10-24 07:41:36

您是否尝试过向其他地址发出请求?它可以将响应服务器端缓存两分钟吗?

Have you tried the request to a different address? Could it be caching the response server side for two minutes?

一片旧的回忆 2024-10-24 07:41:36

您可以尝试使用iOS相关的对象和方法。看到在 MonoTouch 中使用 NSUrlCache 时肯定会实现缓存,并且您可以使用正确的 NSUrlRequestCachePolicy 枚举来绕过它。当我想避免命中缓存时,我是这样做的:

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);

希望这会有所帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文