- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
美好的一天。
我在我的 C# 应用程序中使用 WebClient
类以便每分钟下载相同的文件,然后应用程序执行简单的检查以查看文件是否已更改,以及是否它确实用它做了一些事情。
因为这个文件每分钟下载一次,WebClient
缓存系统正在缓存这个文件,而不是再次下载这个文件,只是简单地从缓存中获取它,这会妨碍检查如果下载的文件是新的。
所以我想知道如何禁用 WebClient
类的缓存系统。
我试过了。
Client.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.BypassCache);
我也试过标题。
WebClient.Headers.Add("Cache-Control", "no-cache");
效果不佳。那么我怎样才能永久禁用缓存呢?
谢谢。
编辑
我还尝试了以下 CacheLevels
:NoCacheNoStore
、BypassCache
、Reload
。没有效果,但是如果我重新启动我的计算机,缓存似乎被清除了,但我不能每次都重新启动计算机。
UPDATE in face of recent activity (8 Set 2012)
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.
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.
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.
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.
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.
最佳答案
您可以尝试在每次下载文件时将一些随机数作为查询字符串的一部分附加到您的网址。这确保了 url 每次都是唯一的。
对于前
Random random = new Random();
string url = originalUrl + "?random=" + random.Next().ToString();
webclient.DownloadFile(url, downloadedfileurl);
关于C# WebClient 禁用缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3812089/
我正在编写一个类库来在我无法控制的站点上执行操作。该网站正在接受表单帖子作为输入。 谁能告诉我这两种方法除了上传数据的形式之外是否有区别? System.Net.WebClient.Uploa
用于工作的代码。 有问题的网址是 https://yobit.net/api/3/info 它适用于 IE。它曾经与 webclient 一起使用。它现在在 webclient 中不起作用。我想知道问
因此,我将我的 WebClient 包装在一个 using 语句中。但是我突然想知道,如果我的对象实现了 IDisposable 并且包装在 using 语句中,我是否需要取消订阅事件? 下面是我当前
我正在 VS15 测试版中工作并尝试使用 WebClient。虽然 System.Net 被引用,并且智能感知建议 WebClient 类可用,但在构建时我收到以下错误: The type or na
我想知道是否可以将 cookie 从一个 Web 客户端复制到另一个 Web 客户端。 原因 我正在使用并行 Web 请求,它会在每个新线程上创建 Web 客户端的新实例。 问题 信息敏感,需要使用p
我正在尝试使用 WebClient,但它给我错误,所以我检查了几个论坛(包括这个),他们告诉我把它放在哪里 在文件的顶部: using System.Net 在我想使用 WebClient 的地方之后
我正在尝试使用 WebClient 实现以下场景。使用 RestTemplate 很简单,但我不能再这样做了。 伪java代码中Spring Controller 的相关部分: Mono t1 = w
我正在使用 Spring WebClient 调用休息服务。如下所述的 post 调用代码。 Mono response = client.post()
正在尝试使用 WebClient在 Blazor 项目中。 得到以下错误: 在 blazor.webassembly.js:1 WASM: System.Net.WebException: An ex
我正在使用 ASP.NET Core 并尝试将文件下载到绝对路径。 但我遇到的问题是文件总是被下载到项目目录,文件名本身得到整个路径的名称。 我的代码: string path = @"C:\User
我需要自动化涉及使用登录表单的网站的流程。我需要在登录页面之后的页面中捕获一些数据。 我知道如何对普通页面进行屏幕抓取,但不知道如何抓取安全站点背后的页面。 这可以通过 .NET WebClient
我正在尝试逐步下载一系列序列化数据。目标是从服务器发送一个大块,并在下载时在客户端对其进行部分处理。 我正在使用 System.Net.WebClient 类并将其 AllowReadStreamBu
我在 Windows 桌面应用程序上使用此代码来获取组合框的值,之后我需要选择哪个组合框将使用 JavaScript 使用新信息更新页面 private WebBrowser withEventsFi
我正在尝试通过 C# 代码获取网站的 HTML 源代码。当我使用 Windows 身份验证访问站点时,以下代码有效: using (WebClient client = new WebClient()
我只是使用WebClient.DownloadString(),但速度慢得惊人。最大速度为 40kbs 我尝试将 WebClient.Proxy 设置为 null,但这不起作用,而且我还没有达到最大互
为了利用新的 WebClient API,我在我的 Intellij 项目中包含了 spring-webflux。 dependencies { implementation 'org.spr
我已经开始使用 WebClient,并使用过滤器方法添加请求/响应日志记录: WebClient.builder() .baseUrl(properties.getEndpoint())
我正在使用 WebClient.DownloadFile 将图像下载到本地存储库,如下所示: WebClient myWC = new WebClient();
我尝试使用网络客户端非阻塞方法验证验证码响应。所以它的工作,但我需要我的方法返回 boolean 值而不是异常。我如何从订阅返回值? webClient
这个问题已经有答案了: What does a "Cannot find symbol" or "Cannot resolve symbol" error mean? (18 个回答) 已关闭 3 年
我是一名优秀的程序员,十分优秀!