1

I have a problem with my .NET Maui app, so I have a HttpService class where I can easily fetch the response of a Post/Get request. But when run my app on my local Android device and do a "Post" and have a body I get this error:

    {System.ObjectDisposedException: Cannot access a closed Stream.
   at System.IO.MemoryStream.EnsureNotClosed()
   at System.IO.MemoryStream.CopyToAsync(Stream destination, Int32 bufferSize, CancellationToken cancellationToken)
   at Xamarin.Android.Net.AndroidMessageHandler.WriteRequestContentToOutput(HttpRequestMessage request, HttpURLConnection httpConnection, CancellationToken cancellationToken) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Xamarin.Android.Net/AndroidMessageHandler.cs:line 458
   at Xamarin.Android.Net.AndroidMessageHandler.DoProcessRequest(HttpRequestMessage request, URL javaUrl, HttpURLConnection httpConnection, CancellationToken cancellationToken, RequestRedirectionState redirectState) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Xamarin.Android.Net/AndroidMessageHandler.cs:line 533
   at Xamarin.Android.Net.AndroidMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Xamarin.Android.Net/AndroidMessageHandler.cs:line 375
   at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)
   at MyApp.Services.HttpService.PostAsync(String url, String body, String token, Dictionary`2 headers) in C:\Users\user\Documents\temp\MyApp\MyApp\Services\HttpService.cs:line 38}

Here is my method:

public async Task<string> PostAsync(string url, string body = "", string? token = "", Dictionary<string, string> headers = null)
    {
        HttpClient client = new();
        try
        {
            var request = new HttpRequestMessage(HttpMethod.Post, url)
            {
                Content = new StringContent("", Encoding.UTF8, "application/json"),
            };

            if (headers != null)
            {
                foreach (var header in headers)
                {
                    request.Headers.Add(header.Key, header.Value);
                }
            }

            if (token != "") request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);


            var result = await client.PostAsync(url, new StringContent("", Encoding.UTF8, "application/json"));
            //var result = await client.SendAsync(request);

            var test = await result.Content.ReadAsStringAsync();

            if (result.StatusCode != HttpStatusCode.OK)
            {
                throw new Exception("Något gick fel");
            }

            return await result.Content.ReadAsStringAsync();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

I have tried both "SendAsync" and "PostAsync" but none of them work. But SendAsync works if I send the HttpRequestMessage and comment out the "Content = ..." part.

It works flawless if I debug it on my windows machine. I dont have any more android devices to test it on unfortunatley.. I will try the emulator and write the result.

**Same result on the emulator..

Thanks in advance! Best regards Max

Hickori
  • 55
  • 6
  • There is a similar thread, you can check it first [here](https://stackoverflow.com/questions/27715327/when-or-if-to-dispose-httpresponsemessage-when-calling-readasstreamasync). – Jessie Zhang -MSFT Jun 28 '23 at 02:02
  • Thanks! But I dont dispose anything? I have tried putting the sendasync inside a "using" around the httprequestmessage, but it makes no difference, same error... – Hickori Jun 28 '23 at 16:38
  • I FOUND IT, it was because my endpoint wat to an "http://". it had to be https!! – Hickori Jun 28 '23 at 16:47

0 Answers0