HttpClient - Uploading file through API is taking more time in .NETMAUI(.NETCore 8.0) compared to Xamarin.Forms

Venkatareddy Desireddy 0 Reputation points
2025-04-03T09:42:25.7566667+00:00

In MAUI application from Android device, I am uploading file through API via HttpClient into controller.

In Xamarin.Forms upload file is taking 3 minutes of time, but after migrating into .NET MAUI uploading is taking 6 minutes of time. I am suspecting HttpClient might be running slowly in .NET MAUI.

Below is the request structure for your reference.

//Common code

var clientHandler = GetInsecureHandler();

var httpClient = new HttpClient(clientHandler);

httpClient.Timeout = TimeSpan.FromMilliseconds(Timeout.Infinite);

//Android platform specific code

public HttpClientHandler GetInsecureHandler()

{

HttpClientHandler httpClientHandler = new HttpClientHandler();

httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; };

return httpClientHandler;

}

//request message

{Method: GET, RequestUri: 'https://10.xxx.xxx.1/api/upgrade/start', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:

{

Content-Length: 129986313

}}

//API call

var responseMessage = await httpClient.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead, cancellationToken.Token).ConfigureAwait(false);

Please help me figure out issue ASAP.

Developer technologies | .NET | .NET MAUI
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Le (WICLOUD CORPORATION) 10,745 Reputation points Microsoft External Staff Moderator
    2025-08-20T09:12:23.95+00:00

    Hello,

    The performance difference you're seeing is likely due to changes in the underlying networking stack. .NET MAUI uses SocketsHttpHandler, which may negotiate HTTP/2 by default and applies different settings compared to Xamarin.Forms.

    Recommendation

    You could consider the following practices to improve your upload performance:

    1. Choose the correct HTTP method

    • Use POST for creating new resources or PUT for replacing existing ones
    • Avoid GET requests with request bodies, as this violates HTTP standards and can cause issues

    2. Optimize your streaming approach

    • Implement StreamContent with a 64KB buffer size for better performance
    • Use ResponseHeadersRead to prevent buffering the entire response in memory

    3. Test HTTP version differences

    • Try forcing HTTP/1.1 for your upload requests to see if this improves performance
    • HTTP/2's flow control can sometimes throttle long-running streams on certain servers
    • Compare the results to help identify the best configuration for your scenario

    4. Adjust timeout settings

    • Set generous client timeouts (consider 15+ minutes for large files)
    • Ensure your server timeout settings can accommodate the upload duration

    5. Manage connections properly

    • Limit concurrent uploads to avoid connection contention
    • Reuse a single HttpClient instance per host to benefit from connection pooling
    • Consider setting MaxConnectionsPerServer = 1 to dedicate a connection during uploads

    6. Fine-tune the handler (when needed)

    • Disable AutomaticDecompression for upload requests
    • Only use permissive certificate validation in test environments

    7. Consider chunked uploads for large files

    • Implement resumable uploads to improve user experience and handle network interruptions

    I hope this helps you with your issue.

    1 person found this answer helpful.
    0 comments No comments

  2. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,166 Reputation points Microsoft External Staff
    2025-04-04T02:20:29.3733333+00:00

    Hello,

    I noticed that you uploaded the file using the GET request, which is not officially recommended by .net. According to HTTP semantics, the GET request should be used to read server resources, the PUT request and the POST request should be used to modify server resources, and GET and PUT should be idempotent operations, while POST does not guarantee idempotence.

    Therefore, the Post request is usually used to add files, and the Put request is used to modify existing files on the server.

    For your needs of uploading files, you can refer to the following .net commonly used form method to upload files to the server. And test its efficiency difference on Xamarin and Maui.

    
    var filePath = @"file_path";
    
     
    
    using (var multipartFormContent = new MultipartFormDataContent())
    
    {
    
        //Load the file and set the file's Content-Type header
    
        var fileStreamContent = new StreamContent(File.OpenRead(filePath));
    
        fileStreamContent.Headers.ContentType = new MediaTypeHeaderValue("filetype eg:image/png");
    
     
    
        //Add the file
    
        multipartFormContent.Add(fileStreamContent, name: "file", fileName: "filename.xxx");
    
     
    
        //Send it
    
        var response = await httpClient.PostAsync("upload_url", multipartFormContent);
    
        response.EnsureSuccessStatusCode();
    
        return await response.Content.ReadAsStringAsync();
    
    }
    
    

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.