提问人:Dragon Slayer 提问时间:10/12/2023 最后编辑:Dragon Slayer 更新时间:10/14/2023 访问量:124
.Net MAUI 应用中的异常:Android 上的“System.ObjectDisposedException:无法访问关闭的流”
Exception in .Net MAUI App: "System.ObjectDisposedException: Cannot access a closed Stream" on Android
问:
我目前正在开发一个 .Net MAUI 应用程序,我正在使用特定函数进行 HTTP 调用。我面临的特殊问题是该函数在 Windows 上运行良好,但是当我尝试在 Android 上执行它时,它会抛出异常:
“System.ObjectDisposedException:无法访问已关闭的流。”
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 和 DELETE 在两个平台上都有效
像这样的 Post 操作在两个平台上都有效
HttpResponseMessage 响应 = 等待 客户。PostAsync($“{url}/inventory/{id}/{destination}”, null);
但是当我尝试将内容放入请求中时,它不适用于 android,但在 Windows 中有效。
答:
0赞
Dragon Slayer
10/13/2023
#1
我从我的 asp.net APIapp Program.cs文件“//app.使用HttpsRedirection();”
删除后,它起作用了。@unsane 在这个 stackoverflow 中得到的答案之一得到了我的解决方案。
现在,我什至不需要Android的HttpClient中的不安全处理程序。
评论