开发者

WP7 HttpWebRequest without caching

开发者 https://www.devze.com 2023-01-30 17:49 出处:网络
It seems that HttpWebRequest caching in WP7 is enabled by default, how do I turn it off? Add开发者_如何转开发ing a random

It seems that HttpWebRequest caching in WP7 is enabled by default, how do I turn it off? Add开发者_如何转开发ing 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.


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();


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.


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";


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.


I found 3 ways

  1. 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();

  1. 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");
  1. 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>


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.


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消