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