WebClient().DownloadString()返回旧数据

5
我使用这段代码从URL获取返回的字符串。
webClient.Encoding = Encoding.UTF8;
response = webClient.DownloadString("http://somesite.com/code.php");
Console.Write(response);

code.php看起来像这样

<?php
$data = file_get_contents('code.txt');
echo $data;
?>

问题在于当我更改 code.txt 文件的内容时,webClient.DownloadString() 方法会返回旧的 code.txt 文件内容。但是,当我在浏览器中打开 URL http://somesite.com/code.php 时,它可以完美地工作。
如果您有任何解决方案,将不胜感激!
我的问题似乎是重复的,但我并不真正理解这里所说的: C# WebClient disable cache 如果有人能够解释并提供一些示例代码,那就太棒了!

我并不真正理解那个答案。有示例代码吗? - Hussain Noor Mohamed
在我提到的问题的答案中有很多有用的提示。你尝试过在webclient中禁用缓存甚至切换到HttpWebRequest吗? - Andrey Korneyev
1个回答

11

尝试在WebClient上禁用缓存

webClient.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);

WebClient 缓存的 MSDN 文档


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接