【发布时间】:2011-06-13 18:48:08
【问题描述】:
Windows Phone HTTP 客户端会自动缓存吗?
使用 Fiddler 时,我没有看到我的所有请求。
【问题讨论】:
标签: windows-phone-7
Windows Phone HTTP 客户端会自动缓存吗?
使用 Fiddler 时,我没有看到我的所有请求。
【问题讨论】:
标签: windows-phone-7
是的,HttpWeRequest(WebClient 在内部使用它,如果您正在使用它)具有内置缓存,这可能非常激进,具体取决于您的要求。
解决这个问题的常见方法是向查询字符串附加额外的值,这除了解决先前调用的缓存之外没有任何意义。
类似:
var mrUri = "http://something.com/path/file.ext?nocache=" + Guid.NewGuid();
或
var mrUri = "http://something.com/path/file.ext?nocache=" + DateTime.UtcNow.ToString();
【讨论】:
我最近遇到了这个问题。一些关于它的博客文章:
http://ayende.com/blog/4755/silverlight-and-http-and-caching-oh-my
http://www.nickharris.net/2010/10/windows-phone-7-httpwebrequest-returns-same-response-from-cache/
这是我与Spring.NET REST Client Framework 一起使用的代码,默认情况下不强制缓存:
public class NoCacheRequestInterceptor : IClientHttpRequestFactoryInterceptor
{
public IClientHttpRequest Create(IClientHttpRequestFactoryCreation creation)
{
creation.Uri = new Uri(String.Format("{0}{1}nocache={2}",
creation.Uri.ToString(),
String.IsNullOrEmpty(creation.Uri.Query) ? "?" : "&",
DateTime.Now.Ticks.ToString()));
return creation.Create();
}
}
RestTemplate client = new RestTemplate("http://www.example.com/");
client.RequestInterceptors.Add(new NoCacheRequestInterceptor());
// ..
string result = client.GetForObject<string>("category/{id}", 4);
【讨论】: