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

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,126 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 Answers by the question author, which helps users to know the answer solved the author's problem.