No cache with HttpClient in Windows Phone 8

后端 未结 5 399
夕颜
夕颜 2020-12-03 18:40

I\'ve read that in order to disable caching while using get and post methods in HttpClient, I need to use a WebRequestHandler as my HttpClien

5条回答
  •  猫巷女王i
    2020-12-03 19:19

    If using Windows.Web.Http.HttpClient, the clean way to fix this issue from the client side is:

    var httpFilter = new Windows.Web.Http.Filters.HttpBaseProtocolFilter();
    httpFilter.CacheControl.ReadBehavior = 
        Windows.Web.Http.Filters.HttpCacheReadBehavior.MostRecent;
    var httpClient = new Windows.Web.Http.HttpClient(httpFilter);
    

    This way, you avoid filling the app's cache with temp files when using random query strings. Each response is stored in the cache.

    Of course, it is always recommended to fix the issue from the server side. Add the following header, and you won't need to worry about cache on each client:

    Cache-Control: no-cache
    

    Full response:

    HTTP/1.1 200 OK
    Content-Length: 31
    Content-Type: text/plain; charset=UTF-8
    Cache-Control: no-cache
    
    ...
    

提交回复
热议问题