I'm building a .NET MAUI app that communicates with a local server by sending HTTP POST commands. I have a class in a .NET library, and I use HttpClient to send requests. It works perfectly and instantly on iOS, but on Android, sending multiple POST requests in quick succession results in a noticeable delay:
The first command is fast
Subsequent commands take 5+ seconds each
If I wait 5 seconds between commands, each command is again fast
This is my simplified sending function (called with await each time):
internal async Task<bool> SendCommandAsync(string action)
{
try
{
var requestUri = $"https://{ip}:8080/api/command?action={action}";
var request = new HttpRequestMessage(HttpMethod.Post, requestUri);
request.Headers.Add("x-api-key", "xxx");
request.Headers.Add("x-client-token", token);
HttpResponseMessage response = await client.SendAsync(request);
if (response.IsSuccessStatusCode)
{
Console.WriteLine($"Command '{action}' sent successfully.");
return true;
}
else
{
Console.WriteLine($"Error send command: {response.StatusCode} - {await response.Content.ReadAsStringAsync()}");
return false;
}
}
catch (Exception ex)
{
Console.WriteLine($"Exception: {ex.Message}");
return false;
}
}
What I've tried:
- Reusing a single HttpClient instance
- Injecting different handlers (HttpClientHandler, AndroidMessageHandler)
- Forcing HTTP/1.1 (request.Version = new Version(1, 1);)
- Disabling proxies
- Adding detailed timing logs (delay is always inside the await client.SendAsync(request))
- Same server/network: On iOS, works as expected; on Android, delayed after first request
Additional info:
- The Android app and server are on the same WiFi network
- No exceptions are thrown, only delays
- Not reusing any HttpContent/StringContent objects
- Not using fire-and-forget; always awaiting the requests
Questions:
- Why does this only happen on Android?
- Is there an Android-specific setting, handler, or best practice for MAUI that fixes slow/queued POST requests?
- Are there other debugging tips or known issues in .NET MAUI for Android networking I should check?
Any help or ideas appreciated!
SendCommandAsyncrunning on UI thread? If so, I bet you've blocked UI thread while holding (something). What objects is that calling code holding on to? As an experiment, write a very simple app. (Or find an existing simple app that does work.) Put all your code into a background thread (Task.Run(...)the method that starts your sequence of HTTP requests. Stripped down to its essence, should work.async/awaitdoes not magically absolve you of the need to be careful about long-running methods. It is possible that somewhere on your call stack, there is a problem. Either holding on to something that causes Android to not relinquish some resource, or blocking the UI thread for "too long". This is why it is best to do a clean test "from scratch", that does the minimum required to investigate HTTP behavior. Create a minimal "working" solution. Gradually enhance that towards what you need. When you hit a wall, you'll have a good question to ask.public github repois useful. Try to include a bit more code in your question. But I'm wagering the problem won't be in the code snippet posted in question; it'll be in how you got there. That's what I mean by "somewhere in the call stack".