WP7 HttpWebRequest without caching

前端 未结 7 2138
北荒
北荒 2020-12-01 09:53

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

相关标签:
7条回答
  • 2020-12-01 10:04

    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.

    0 讨论(0)
  • 2020-12-01 10:16

    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.

    0 讨论(0)
  • 2020-12-01 10:20

    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";
    
    0 讨论(0)
  • 2020-12-01 10:21

    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>
    
    0 讨论(0)
  • 2020-12-01 10:23

    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.

    0 讨论(0)
  • 2020-12-01 10:26

    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 讨论(0)
提交回复
热议问题