1

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:

  1. Why does this only happen on Android?
  2. Is there an Android-specific setting, handler, or best practice for MAUI that fixes slow/queued POST requests?
  3. Are there other debugging tips or known issues in .NET MAUI for Android networking I should check?

Any help or ideas appreciated!

4
  • It sounds like you're describing a throttling behavior. It's not that request takes 5 seconds, its that each request is being blocked for 5 seconds. It is as, you said, the actual request is quick. If you have some control of the end-point, you could reengineer the end point to accept a multiple inputs and provide multiple outputs, or you could spread your queries over multiple alternate end-points. Commented Jun 30 at 22:25
  • Of all the approaches you've mentioned, "Reusing a single HttpClient instance" is the only detail you should need. Perhaps something is being held on to? Is the code that calls SendCommandAsync running 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. Commented Jul 5 at 0:19
  • It is essential to understand that using async/await does 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. Commented Jul 5 at 0:25
  • ... If you encounter this 5 second delay even with a tiny app, then you'll have a minimal example that you can post as the question. In that case, linking to the code as a public github repo is 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". Commented Jul 5 at 0:32

1 Answer 1

1

I found the solution!

The problem is with the default HttpClientHandler on Android—it causes 5+ second delays between rapid HTTPS requests to local servers, due to the way it manages sockets and connection pooling.

The fix:
Use SocketsHttpHandler instead of HttpClientHandler on Android. This handler avoids the delay and gives you instant responses, just like on iOS.

Example:

        var handler = new SocketsHttpHandler
        {
            SslOptions = { RemoteCertificateValidationCallback = (sender, cert, chain, errors) => true },
            PooledConnectionLifetime = TimeSpan.FromMinutes(10),
            PooledConnectionIdleTimeout = TimeSpan.FromMinutes(2),
            MaxConnectionsPerServer = 8
        };

        return new HttpClient(handler);
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.