0

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?)
6
  • Are you using the AndroidClientHandler for HttpClient? As far as System.Net.Cache, add a reference to System.Net in your project. Commented Dec 6, 2017 at 5:51
  • I am using HttpClientOnly because all my client resides in on the common project. Therefore I'd prefer a platform agnostic solution if it exists. Still, I will check the AndroidClientHandler you mention: thank you! Commented Dec 6, 2017 at 5:57
  • You can set AndroidClientHandler as a build option in your Xamarin.Android project options (under Android Build / HttpClient Implementation) Commented Dec 6, 2017 at 6:04
  • By the way, I am using HttpWebRequest directly right now (I updated my question). Looks like you suggest me to use HttpClient instead. Do you have some link with straight-forward sample code for using WebClient + AndroidClientHandler? Commented Dec 6, 2017 at 6:19
  • I personally use HttpClient over HttpWebRequest, 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 for HttpWebRequest (still listed as Experimental) so at least security is not a concern like it was. There is no AndroidClientHandler equivalent for WebClient. I'm assuming your "common project" is PCL-based? If you convert to NetStandard2.0 you can access RequestCachePolicy and create one with RequestCacheLevel.NoCacheNoStore and to apply it to WebClient.CachePolicy. Commented Dec 6, 2017 at 6:47

1 Answer 1

1

Regardless what is the best way to hit an HTTP API (WebClient vs. HttpClient vs. HttpWebRequest), the comment from @ShushiHangover help we to figure out the point of my question.

The PCL output format used to be the norm for Xamarin "portable" projects. However, the namespace System.Net.Cache is not usable in PCL libraries. It is available in .NET Standard 2.0, which itself is not usable in PCL. PCL has been deprecated in summer 2017 in favor of .NET standard libraries, that allows to access to the .NET Standard 2.0 (among other benefits).

There is however no way to "convert" a project from PCL to .NET standard library but to make a new project and move all your files. Therefore I decided to create a new .NET standard library project that contains only my HTTP client code.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.