From 4dc84baeb62e3830bfa92b5bbf32d588b85b2e79 Mon Sep 17 00:00:00 2001 From: Marek Habersack Date: Wed, 13 Apr 2016 09:12:08 +0200 Subject: [PATCH] Support custom Xamarin Android HttpClient handler Make it possible to select a default System.Net.HttpClient handler on Android. Selection of the handler is handled on the Xamarin Android side. --- mcs/class/System.Net.Http/Makefile | 3 ++ .../System.Net.Http/HttpClient.android.cs | 38 +++++++++++++++++++ .../monodroid_System.Net.Http.dll.sources | 2 + 3 files changed, 43 insertions(+) create mode 100644 mcs/class/System.Net.Http/System.Net.Http/HttpClient.android.cs create mode 100644 mcs/class/System.Net.Http/monodroid_System.Net.Http.dll.sources diff --git a/mcs/class/System.Net.Http/Makefile b/mcs/class/System.Net.Http/Makefile index bf1cbbd2799..9abcf2e80f6 100644 --- a/mcs/class/System.Net.Http/Makefile +++ b/mcs/class/System.Net.Http/Makefile @@ -6,6 +6,9 @@ LIBRARY = System.Net.Http.dll LIB_REFS = System.Core System LIB_MCS_FLAGS = $(EXTRA_LIB_MCS_FLAGS) +ifeq (monodroid,$(PROFILE)) +LIB_MCS_FLAGS += -d:XAMARIN_MODERN +endif TEST_LIB_REFS = System System.Core TEST_MCS_FLAGS = diff --git a/mcs/class/System.Net.Http/System.Net.Http/HttpClient.android.cs b/mcs/class/System.Net.Http/System.Net.Http/HttpClient.android.cs new file mode 100644 index 00000000000..f48b49cd104 --- /dev/null +++ b/mcs/class/System.Net.Http/System.Net.Http/HttpClient.android.cs @@ -0,0 +1,38 @@ +using System; +using System.Reflection; + +namespace System.Net.Http { + public partial class HttpClient { + + public HttpClient () + : this (GetDefaultHandler (), true) + { + } + + static HttpMessageHandler GetDefaultHandler () + { + Type type = Type.GetType("Android.Runtime.AndroidEnvironment, Mono.Android"); + if (type == null) + return GetFallback ("Invalid Mono.Android assembly? Cannot find Android.Runtime.AndroidEnvironment"); + + MethodInfo method = type.GetMethod ("GetHttpMessageHandler", BindingFlags.Static | BindingFlags.NonPublic); + if (method == null) + return GetFallback ("Your Xamarin.Android version does not support obtaining of the custom HttpClientHandler"); + + object ret = method.Invoke (null, null); + if (ret == null) + return GetFallback ("Xamarin.Android returned no custom HttpClientHandler"); + + var handler = ret as HttpMessageHandler; + if (handler == null) + return GetFallback ($"{ret?.GetType()} is not a valid HttpMessageHandler"); + return handler; + } + + static HttpMessageHandler GetFallback (string message) + { + Console.WriteLine (message + ". Defaulting to System.Net.Http.HttpClientHandler"); + return new HttpClientHandler (); + } + } +} diff --git a/mcs/class/System.Net.Http/monodroid_System.Net.Http.dll.sources b/mcs/class/System.Net.Http/monodroid_System.Net.Http.dll.sources new file mode 100644 index 00000000000..acaa6a38011 --- /dev/null +++ b/mcs/class/System.Net.Http/monodroid_System.Net.Http.dll.sources @@ -0,0 +1,2 @@ +#include System.Net.Http.dll.sources +System.Net.Http/HttpClient.android.cs -- 2.25.1