Skip to content

HttpClient.Send not supported in MAUI running on Android or iOS causing PlatformNotSupportedException #104716

@zijianhuang

Description

@zijianhuang

Description

HttpClient.Send is not supported in a MAUI app running on Android or iOS, though on Windows, it is OK.

Reproduction Steps

		public DemoWebApi.Controllers.Client.Hero[] GetHeroes(Action<System.Net.Http.Headers.HttpRequestHeaders> handleHeaders = null)
		{
			var requestUri = "api/Heroes";
			using var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, requestUri);
			handleHeaders?.Invoke(httpRequestMessage.Headers);
			var responseMessage = client.Send(httpRequestMessage);
			try
			{
				responseMessage.EnsureSuccessStatusCodeEx();
				if (responseMessage.StatusCode == System.Net.HttpStatusCode.NoContent) { return null; }
				var stream = responseMessage.Content.ReadAsStream();
				return JsonSerializer.Deserialize<DemoWebApi.Controllers.Client.Hero[]>(stream, jsonSerializerSettings);
			}
			finally
			{
				responseMessage.Dispose();
			}
		}

Checkout branch: https://github.com/zijianhuang/DemoCoreWeb/tree/HttpClientSend_Bug
And run one of the MAUI mobile app:

  • MauiHeroes.Droid
  • MauiHeroes.IOS
  • Fonlow.MauiHeroes on Android or iOS

Expected behavior

Expected the codes work like "HttpClient.SendAsync(...).Result".

Actual behavior

Error:

System.PlatformNotSupportedException: 'Operation is not supported on this platform.'

When running client.Send(httpRequestMessage)

Regression?

Related to dotnet/maui#10470

Known Workarounds

client.SendAsync(httpRequestMessage).Result;

Configuration

latest .NET 8 as of 2024-07-11
latest MAUI dependencies

Other information

And in https://github.com/dotnet/runtime/blob/main/src/libraries/System.Net.Http/src/System/Net/Http/HttpClient.cs,

 [UnsupportedOSPlatform("browser")]
        public HttpResponseMessage Send(HttpRequestMessage request) =>
            Send(request, DefaultCompletionOption, cancellationToken: default);

Respective prototypes should have got Android and iOS included in UnsupportedOSPlatform attributes. And it will be even better that the implementation of Send is supported in Android and iOS.

Metadata

Metadata

Assignees

Labels

area-System.Net.HttpenhancementProduct code improvement that does NOT require public API changes/additionshelp wanted[up-for-grabs] Good issue for external contributors

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions