No cache with HttpClient on Windows Phone 8

I read that in order to disable caching when using the get and post methods in HttpClient , I need to use WebRequestHandler as my HttpClient HttpClientHandler and change its caching policy. However, WebRequestHandler is not located in System.Net.Http.dll, but rather in System.Net.Http.WebRequest.dll, so I tried to add the DLL to the project as a reference. I got an error message:

Microsoft Visual Studio

Link to a higher version or incompatible assembly cannot be added to the project.

Again, after a short search, I came to the conclusion that the .dll file was blocked because it was downloaded from another source. To unlock it, I kept trying the solution here . However, this did not work either, and I still get the same error when I try to add the .dll file as a link.

All I want to do is disable caching using my HttpClient, am I doing something wrong here? I am open to any advice or help.

My system is Windows 8.1, and I'm using Visual Studio 2013. The project I'm working on is an application for Windows Phone 8. The .dll directory I'm trying to link to is "C: \ Windows \ Microsoft.NET \ Framework \ v4.0.30319 \ System.Net.Http.WebRequest.dll ". Thank you in advance.

+7
c # windows-phone-8
source share
5 answers

You cannot reference regular .NET assemblies in a Windows Phone 8 project. You can only use the .NET API for Windows Phone. This is a subset of regular .NET. See http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj207211%28v=vs.105%29.aspx for more details.

The default caching for HttpClient (and HttpWebRequest) can be handled by adding a value to the query string. For example, a guide.

 string uri = "http://host/path?cache=" + Guid.NewGuid().ToString(); 

The best solution, as stated in the comment above, is to set the "If-Modified-Since" header. HttpWebRequest has a built-in interface:

 HttpWebRequest request = HttpWebRequest.CreateHttp(url); if (request.Headers == null) request.Headers = new WebHeaderCollection(); // Make sure that you format time string according RFC. // Otherwise setting header value will give ArgumentException for culture like 'ti-ER' request.Headers[HttpRequestHeader.IfModifiedSince] = DateTime.UtcNow.ToString("r"); 

But you can add the header manually using HttpClient, I think.

+26
source

If you are using Windows.Web.Http.HttpClient , clear the client side fix for this problem:

 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 populating the application cache with temporary files when using random query strings. Each response is stored in the cache.

Of course, it is always recommended to fix the problem on the server side. Add the following header and you won’t have to worry about the cache on each client:

 Cache-Control: no-cache 

Full answer:

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

I found 3 ways

  • 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 every time

string uri = " http://host.com/path?cache= " + Guid.NewGuid (). ToString ();

  1. Do not specify a cache in the OutgoingResponse header in your WCF service:
 var __request = (HttpWebRequest)WebRequest.Create(url.ToString()); if (__request.Headers == null) __request.Headers = new WebHeaderCollection(); __request.Headers.Add("Cache-Control", "no-cache"); 
  1. label your service 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> 
+7
source

It is for a Windows phone to receive fresh data instead of collecting data

 Using(HttpClient httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.IfModifiedSince = DateTimeOffset.Now; //your code goes here`enter code here` } 

another alternative installed

 Response.Cache.SetCacheability(HttpCacheability.NoCache); 

on server page Page_Load if u get data from aspx page

+5
source

I wrote an HttpMessageHandler based on the solution above:

 public class BypassCacheHttpRequestHandler : HttpClientHandler { protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { if (request.Headers.IfModifiedSince == null) request.Headers.IfModifiedSince = new DateTimeOffset(DateTime.Now); return base.SendAsync(request, cancellationToken); } } 

Use new HttpClient(new BypassCacheHttpRequestHandler(), true); to initialize HttpClient , then you can always bypass the cache.

+4
source

All Articles