Folgendes Code Beispiel zeigt, wie man in WPF den Cache eines HttpClients deaktiviert.
Problem:
unter WPF und Winforms ist das abschalten des Caches in HttpClients und WebRequests immer wieder problematisch.
Dadurch werden URL Adressen trotz verschiedener Adress-Querystring Parametern immer gleich eingelesen.
LΓΆsung:
Man kann den Windows 10 Core httpClient verwenden. In diesem httpClient wird der Cache stabil abgeschaltet.
Integrate the function under the program file:
To do this, you must now include the namespace of Windows 10 in the header
//< Windows 10 > using Windows.Web.Http; //</ Windows 10 > |
Then you can use Windows 10 UWP methods
Here in the example a Window Core httpClient
(because deactivating the cache works here)
//--< Read HTML with httpClient >-- //-< HttpClient >- HttpBaseProtocolFilter filter = new HttpBaseProtocolFilter(); filter.CacheControl.ReadBehavior = HttpCacheReadBehavior.NoCache; filter.CacheControl.WriteBehavior = HttpCacheWriteBehavior.Default;
Windows.Web.Http.HttpClient client = new Windows.Web.Http.HttpClient(filter); //-</ HttpClient >-
//< read HTML > string sHTML = ""; //Client Request as string try { sHTML = await client.GetStringAsync(new Uri(sURL)); } catch (Exception ex) { //clsSys.show_Message(ex.Message); clsSys.fx_Log("Error httpClient: " + ex.Message); return null; } //</ read HTML > //--</ Read HTML with httpClient >--
|
Anhang: folgende klassische Cache-Anweisungen in WPF haben alle nicht funktioniert (Stand 2018)
//< HttpClient > WebRequestHandler handler = new WebRequestHandler(); //*< not working > //0: //*handler.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore); //.BypassCache); //*</ not working > handler.AllowAutoRedirect = false; HttpClient httpClient = new HttpClient(handler); //httpClient.DefaultRequestHeaders.Add("Cache-Control", "no-cache, no-store, must-revalidate"); //httpClient.DefaultRequestHeaders.Add("Cache-Control", "public"); //httpClient.DefaultRequestHeaders.Add("Cache-Control", "private");
//*< not working > //1: //*httpClient.DefaultRequestHeaders.Add("Cache-Control", "no-cache");
//2: //*httpClient.DefaultRequestHeaders.CacheControl.NoStore = true;
//3: //CacheControlHeaderValue cacheControl = new CacheControlHeaderValue(); //cacheControl.NoCache = true; //httpClient.DefaultRequestHeaders.CacheControl = cacheControl;
//*</ not working >
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 > //-</ init >-
//< WebRequest and Response >
WebRequest objRequest = WebRequest.Create(sURL); objRequest.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore); HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse(); //</ WebRequest and Response > //< Stream and Reader > Stream objDataStream = objResponse.GetResponseStream(); StreamReader TextReader = new StreamReader(objDataStream); //</ Stream and Reader > |