It seems that HttpWebRequest caching in WP7 is enabled by default, how do I turn it off? Adding a random param url + "?param=" + RND.Next(10000) works, but it's quite tricky and I'm not sure if it will work with all servers.
问题:
回答1:
For future reference , this worked for me ( I could not use additional query parameter due to project requirements) :
HttpWebRequest request = HttpWebRequest.CreateHttp(url);
if (request.Headers == null)
{
request.Headers = new WebHeaderCollection();
}
request.Headers[HttpRequestHeader.IfModifiedSince] = DateTime.UtcNow.ToString();
回答2:
In case of HttpClient (Portable for Windows Phone) "Cache-Control": "no-cache" on server side works only sometimes. And I cannot add query string random value to RESTful api call as well.
Solution from @frno works good and looks like for HttpClient:
client.DefaultRequestHeaders.IfModifiedSince = DateTime.UtcNow;
Thank you.
回答3:
How do you know it's the phone, not the server (or a proxy somewhere between) which is caching?
Have you checked this with Fiddler2 (or equivalent)?
Have you tried setting headers to disable caching?
Something like:
myRequest = (HttpWebRequest)WebRequest.Create(myUri);
myRequest.Headers["Cache-Control"] = "no-cache";
myRequest.Headers["Pragma"] = "no-cache";
回答4:
We've seen the same behaviour with Silverlight hosted in Chrome.
We add a "?nocache=" + DateTime.Now.Ticks.ToString()
to our request URLs if we want to prevent caching.
回答5:
I found 3 ways
- Add a random Query String to the end of your URI (think Guid.NewGuid()) this will avoid caching on the client as the Query String will be different each time
string uri = "http://host.com/path?cache="+Guid.NewGuid().ToString();
- Specify no cache in the OutgoingResponse header within your WCF service operation:
var __request = (HttpWebRequest)WebRequest.Create(url.ToString()); if (__request.Headers == null) __request.Headers = new WebHeaderCollection(); __request.Headers.Add("Cache-Control", "no-cache");
- markup your service operation with the AspNetCacheProfile attribute:
[AspNetCacheProfile("GetContent")] public ResultABC GetContent(string abc) { __request = (HttpWebRequest)WebRequest.Create(abc); return __request; }
And update your web.config
<system.web> <caching> <outputCache enableOutputCache="true" /> <outputCacheSettings> <outputCacheProfiles > <add name="GetContent" duration="0" noStore="true" location="Client" varyByParam="" enabled="true"/> </outputCacheProfiles> </outputCacheSettings> </caching> ... </system.web>
回答6:
Adding random number is not bad and it will work. I have used Time (in ajax call). Was placed in the url like a folder.
回答7:
Yes is possible... :) I spend one week of Experiment and the answer is really simple :
HttpWebRequest _webRequest = WebRequest.CreateHttp(_currentUrl);
_webRequest.AllowReadStreamBuffering = false
_webRequest.BeginGetResponse(_onDownload,
userState);