Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

How to turn off the HttpClient cache in a WPF application?

03.08.2018 (👁327)


 

Problem: disable HttpClient Cache

If you build a HttpClient under WPF, you will not be able to disable the cache by default.

Thus, when reading web pages in WPF automatically, the same page is often called as http Document.

 

Solution:

Create a WebRequestHandler in WPF and assign it a CachePolicy with NoCacheNoStore

WebRequestHandler handler = new WebRequestHandler();

handler.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.BypassCache);//.NoCacheNoStore);

 

 

namespace

using System.Net.Http;          //*HttpClientHandler WPF

using System.Net.Cache;         //*disable Cache

 

 

Example of the HttpClient WebRequest

//< HttpClient >

WebRequestHandler handler = new WebRequestHandler();

handler.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.BypassCache);//.NoCacheNoStore);

//HttpRequestCachePolicy requestPolicy =new HttpRequestCachePolicy(HttpCacheAgeControl.MaxAge,TimeSpan.Zero);

handler.AllowAutoRedirect = true;           

HttpClient httpClient = new HttpClient(handler);

 

string sHTML = "";   //Client Request as string

try

{

    sHTML = await httpClient.GetStringAsync(new Uri(sURL));

}

catch (Exception ex)

{

    //clsSys.show_Message(ex.Message);

    clsSys.fx_Log("Error httpClient: " + ex.Message);

    return null;

}

//</ HttpClient >