C# WebClient 禁用缓存 [英] C# WebClient disable cache

查看:19
本文介绍了C# WebClient 禁用缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天.

我在我的 C# 应用程序中使用 WebClient 类,以便每分钟下载同一个文件,然后应用程序执行一个简单的检查以查看文件是否已更改,如果它确实用它做一些事情.

I'm using the WebClient class in my C# application in order to download the same file every minute, and then the application performs a simple check to see if the file has been changed, and if it does do something with it.

好吧,因为这个文件每分钟下载一次,WebClient 缓存系统正在缓存该文件,并且不再下载该文件,只是简单地从缓存中获取它,这会妨碍检查如果下载的文件是新的.

Well since this file is downloaded every minute the WebClient caching system is caching the file, and not downloading the file again, just simply getting it from the cache, and that gets in the way of checking if the file downloaded is new.

所以我想知道如何禁用WebClient 类的缓存系统.

So i would like to know how can disable the caching system of the WebClient class.

我试过了.

Client.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.BypassCache);

我也尝试过标题.

WebClient.Headers.Add("Cache-Control", "no-cache");

效果也不好.那么我如何才能永久禁用缓存?

Didn't work as well. So how can i disable the cache for good?

谢谢.

编辑

我还尝试了以下 CacheLevels:NoCacheNoStoreBypassCacheReload.没有效果,但是如果我重新启动计算机,缓存似乎被清除了,但我不能每次都重新启动计算机.

I also tried the following CacheLevels: NoCacheNoStore, BypassCache, Reload. No effect, however if i reboot my computer the cache seems to be cleared, but i can't be rebooting the computer every time.

针对近期活动的更新(8 Set 2012)

标记为已接受的答案解决了我的问题.简单来说,我用用于下载文件的套接字解​​决了我的问题.基本上是一个 GET请求所需的文件,我不会详细介绍如何做,因为我相信你可以在 SO 上找到很多如何做"为了自己做同样的事情.虽然这并不意味着我的解决方案也最适合您,但我的第一个建议是阅读其他答案,看看是否有用.

The answer marked as accepted solved my issue. To put it simple, I used Sockets to download the file and that solved my issue. Basically a GET request for the desired file, I won't go into details on how to do it, because I'm sure you can find plenty of "how to" right here on SO in order to do the same yourself. Although this doesn't mean that my solution is also the best for you, my first advice is to read other answers and see if any are useful.

无论如何,由于这个问题已经看到了一些最近的活动,我考虑添加此更新以包含一些我认为遇到类似问题的人应该考虑的提示或想法他们能想到的一切,并确定问题不在于用他们的代码.可能是大多数情况下的代码,但有时我们只是不太清楚,只需走一走,几分钟后回来,您可能会看到它像最首先是显而易见的事情.

Well anyway, since this questions has seen some recent activity, I thought about adding this update to include some hints or ideas that I think should be considered by those facing similar problems who tried everything they could think off, and are sure the problem doesn't lie with their code. Likely to be the code for most cases, but sometimes we just don't quite see it, just go have a walk and come back after a few minutes, and you will probably see it point blank range like it was the most obvious thing in the first place.

无论哪种方式,如果您确定,那么在这种情况下,我建议检查您的请求是否通过其他具有缓存功能的设备(计算机、路由器、代理等),直到它到达预期目的地.

Either way if you're sure, then in that case I advise to check weather your request goes through some other device with caching capabilities (computers, routers, proxies, ...) until it gets to the intended destination.

考虑到大多数请求都通过前面提到的一些此类设备,更多通常是路由器,当然,除非您直接连接到通过您的服务提供商上网网络.

Consider that most requests go through some of such devices mentioned before, more commonly routers, unless of course, you are directly connected to the Internet via your service provider network.

有一次我自己的路由器缓存了文件,我知道很奇怪,但情况确实如此,每当我重新启动它或直接连接到 Internet 时,我的缓存问题就消失了.不,没有任何其他设备连接到路由器可以归咎于,只有计算机和路由器.

In one time my own router was caching the file, odd I know, but it was the case, whenever I rebooted it or connected directly to the Internet my caching problem went away. And no there wasn't any other device connected to the router that can be blamed, only the computer and router.

顺便说一下,这是一个一般性建议,尽管它主要适用于在公司开发计算机而不是自己的计算机上工作的人.您的开发计算机是否可以运行某种缓存服务?这是可能的.

And by the way, a general advice, although it mostly applies to those who work in their company development computers instead of their own. Can by any change your development computer be running a caching service of sorts? It is possible.

此外考虑到许多高端网站或服务使用内容交付网络 (CDN),并且取决于 CDN 提供商,每当更新或更改文件时,都需要一些时间变化反映在整个网络中.因此它可能是可能你不走运要求一个可能是在更新过程中,以及离您最近的 CDN 服务器还没更新完.

Furthermore consider that many high end websites or services use Content Delivery Networks (CDN), and depending on the CDN provider, whenever a file is updated or changed, it takes some time for such changes to reflect in the entire network. Therefore it might be possible you were in the bad luck of asking for a file which might be in a middle of a update, and the closest CDN server to you hasn't finished updating.

无论如何,特别是如果您总是一遍又一遍地请求相同的文件,或者如果您找不到问题所在,那么如果可能的话,我建议您重新考虑您的方法一次又一次地请求相同的文件,而是考虑构建一个简单的 Web 服务,以满足您首先考虑满足此类文件的需求.

In any case, specially if you are always requesting the same file over and over, or if you can't find where the problem lies, then if possible, I advise you to reconsider your approach in requesting the same file time after time, and instead look into building a simple Web Service, to satisfy the needs you first thought about satisfying with such file in the first place.

如果您正在考虑这样的选择,我认为您可能会更轻松地构建 REST 根据您自己的需要设置 Web API 样式.

And if you are considering such option, I think you will probably have a easier time building a REST Style Web API for your own needs.

我希望这个更新在某种程度上对你有用,肯定对我有用.祝您在编码工作中好运.

I hope this update is useful in some way to you, sure it would be for me while back. Best of luck with your coding endeavors.

推荐答案

从上面我猜你在别的地方有问题.您可以在服务器端记录 http 请求吗?当你改变一些随机种子参数时你会得到什么?

From the above I would guess that you have problem somewhere else. Can you log http requests on server side? What do you get when you alter some random seed parameter?

也许 SERVER 缓存了文件(如果日志显示请求确实每分钟触发一次.

Maybe SERVER caches the file (if the log shows that request is really triggered every minute.

您使用 ISA 还是 SQUID?

Do you use ISA or SQUID?

您的请求的 http 响应代码是什么?

What is http response code for your request?

我知道用答案回答可能不受欢迎,但评论不允许我写这么多文本:)

I know that answering with answers might not be popular, but comment doesn't allow me this much text :)

无论如何,使用HttpRequest 对象代替WebClient,希望(如果您对WebClient 表示怀疑)一切都会得到解决.如果使用 HttpRequest 没有解决问题,那么问题就出在其他地方.

Anyway, use HttpRequest object instead of WebClient, and hopefully (if you place your doubts in WebClient) everything will be solved. If it wasn't solved with HttpRequest, then the problem really IS somewhere else.

进一步细化:

更低:我如何创建一个在 .Net 中手动请求 HTTP 请求?

这是纯套接字,如果问题仍然存在,则打开一个新问题并将其标记为WTF :)

This is pure sockets, and if the problem still persists, then open a new question and tag it WTF :)

这篇关于C# WebClient 禁用缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆