I am using the namespace System.Net.Http namespace with Xamarin in order to make calls to my HTTP REST API and it works fine. My http client code resides in the common portable project, and I call it from my android-only project (iOS is still in the backlogs)
Below is how it looks like:
Uri apiUri = new Uri("/example");
var request = (HttpWebRequest)HttpWebRequest.Create(apiUri);
request.ContentType = "application/json";
request.Method = "GET";
WebResponse response = await request.GetResponseAsync();
However, I suspect that consecutive GET to the same URL are cached. I have seen that it possible to remove the cache through HttpRequestCachePolicy objects, as described in https://developer.xamarin.com/api/type/System.Net.Cache.HttpRequestCachePolicy/ . My could should then look like:
Uri apiUri = new Uri("/example");
var request = (HttpWebRequest)HttpWebRequest.Create(apiUri);
request.ContentType = "application/json";
request.Method = "GET";
HttpRequestCachePolicy noCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
request.CachePolicy = noCachePolicy;
WebResponse response = await request.GetResponseAsync();
However, the namespace System.Net.Cache is not defined anywhere and I cannot use this method. I am using the .NET SDK 4.5.
Is there some settings I am missing? Or should I look for a different way to disable GET request caching?
Below is the error I get:
The type or namespace name 'Cache' does not exist in the namespace 'System.Net' (are you missing an assembly reference?)
AndroidClientHandlerforHttpClient? As far asSystem.Net.Cache, add a reference toSystem.Netin your project.AndroidClientHandleryou mention: thank you!AndroidClientHandleras a build option in yourXamarin.Androidproject options (under Android Build / HttpClient Implementation)HttpClientoverHttpWebRequest, there are plenty of SO Q&As and blog posts covering the "why". Xamarin.Android (7.1) added native TLS1.2 (via Google's BoringSSL) as a build option forHttpWebRequest(still listed asExperimental) so at least security is not a concern like it was. There is noAndroidClientHandlerequivalent forWebClient. I'm assuming your "common project" is PCL-based? If you convert to NetStandard2.0 you can accessRequestCachePolicyand create one withRequestCacheLevel.NoCacheNoStoreand to apply it toWebClient.CachePolicy.