SOLVED

Exception in .Net MAUI App: "System.ObjectDisposedException: Cannot access a closed Stream" on Andro

Copper Contributor

I am currently developing a .Net MAUI application where I am making an HTTP call. The peculiar issue I am facing is that the function works perfectly fine on Windows, but when I attempt to execute it on my Local Android Device, it throws an exception after line var response = await client.PostAsync(url, content);

 

> "System.ObjectDisposedException: Cannot access a closed Stream."

 

 

```

public async static Task InsertParts(IEnumerable<PartDTO> partsToReport)

{

using (HttpClient client = GetClient())

{

AddAuthorizationHeader(client);

string url = $"{Url}/endpoint";

 

string jsonData = JsonSerializer.Serialize(partsToReport);

HttpContent content = new StringContent(jsonData, Encoding.UTF8, "application/json");

 

var response = await client.PostAsync(url, content);

 

if (!(response.IsSuccessStatusCode))

{

throw new Exception($"Failed to insert parts: {response.ReasonPhrase}");

}

}

}

```

 

```

public static HttpClient GetClient()

{

#if DEBUG

HttpClientHandler insecureHandler = new HttpClientHandler();

insecureHandler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;

return new HttpClient(insecureHandler);

#else

return new HttpClient();

#endif

}

```

GET and DELETE works on both platforms

 

The Post operation like this works on both platforms

> HttpResponseMessage response = await client.PostAsync($"{Url}/inventory/{id}/{destination}", null);

 

but when I try to put content in the request then it does not work on android but works in Windows.

2 Replies
best response confirmed by Dragon_Slayer (Copper Contributor)
Solution
I found the answer I don't know how it works but I removed this single line from my asp.net APIapp Program.cs file "//app.UseHttpsRedirection();" After removing that it worked. one of the answers in this https://stackoverflow.com/questions/71047509/trust-anchor-for-certification-path-not-found-in-a-net-... got my solution but it was just a hunch for me
1 best response

Accepted Solutions
best response confirmed by Dragon_Slayer (Copper Contributor)
Solution
I found the answer I don't know how it works but I removed this single line from my asp.net APIapp Program.cs file "//app.UseHttpsRedirection();" After removing that it worked. one of the answers in this https://stackoverflow.com/questions/71047509/trust-anchor-for-certification-path-not-found-in-a-net-... got my solution but it was just a hunch for me

View solution in original post