我在这里下载了最新的示例MAUI:https://github.com/microsoft/dotnet-podcasts,并复制了它们发出请求的确切方式,但我无法通过"意外的流结束错误":

System.Net.WebException: unexpected end of stream on com.android.okhttp.Address@4c288121
 ---> Java.IO.IOException: unexpected end of stream on com.android.okhttp.Address@4c288121

我正在初始化MauiProgram中的HttpClient.cs通过AddHttpClient出厂方法,如so(注意ANDROID的不同IP地址):

public static string Base = DeviceInfo.Platform == DevicePlatform.Android ? "http://10.0.2.2" : "https://localhost";
public static string APIUrl = $"{Base}:7096/api/";

builder.Services.AddHttpClient<DishClient>(client => { client.BaseAddress = new Uri(APIUrl); });

我在AndroidManifest中包含了以下android:networkSecurityConfig个.xml(允许http通信):

<network-security-config>
  <base-config cleartextTrafficPermitted="true">
        <trust-anchors>
            <certificates src="system" />
        </trust-anchors>
    </base-config>
</network-security-config>

Xamarin中的解决方案似乎是使用AndroidClientHandler,但这是在一个不推荐使用的库中,我无法在MAUI中使用,并且在有效的链接解决方案中没有对它的引用.

Things I've tried:

使用Https:我遵循了此处的指南:https://docs.microsoft.com/en-us/xamarin/cross-platform/deploy-test/connect-to-local-web-services#bypass-the-certificate-security-check,但仍会收到证书错误:

   System.Net.WebException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
   ---> Javax.Net.Ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

我也试过这个

handler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator; 

但是得到一个错误

System.PlatformNotSupportedException: Operation is not supported on this platform

已经连续4天了,我已经判断了工作解决方案中所有可能的设置,没有发现任何差异.有什么 idea 吗?

推荐答案

@Jimmy在您的代码中,当您在android上运行时,您正在将URL从"https://localhost"更改为"http://10.0.2.2".在这两种情况下,您都在端口7096调用API:

我假设您的服务器只在端口7096上侦听HTTPS,而不侦听HTTP.也许这会导致你的问题.

另一个问题可能是您的服务只绑定到localhost,而没有绑定到您的IP地址(您可以在API的launchsettings.json中查找).

另一个问题可能是防火墙不允许端口7096上的传入流量.您也应该判断这一点,因为在Windows上运行时不会跨越计算机边界.但是,在Android emulator上运行时,您可以进行跨机器通信.

.net相关问答推荐

带有ASP.NET核心的Angular 项目模板.API试验

是否存在指定的(子)索引分隔符?

部署时如何控制红隼端口?

使用 MassTransit、.NET Core 和 RabbitMQ 的设计挑战

如何判断属性设置器是否公开

标签从右向左增长

如何从标头中检索基本身份验证凭据?

为什么 SortedSet.GetViewBetween 不是 O(log N)?

在目录中创建应用程序快捷方式

extern 在 C# 中是如何工作的?

带有输入字段的消息框

实体框架 - 无法将 lambda 表达式转换为类型字符串,因为它不是委托类型

向 .NET 应用程序添加脚本功能

在生产环境中部署调试符号(pdb 文件)有什么风险?

在 C# 中,为什么不能将 List 对象存储在 List 变量中

获取磁盘上文件的大小

清除文件内容

嵌套的 Try/Catch 块是个坏主意吗?

obj 文件夹是为了什么而生成的?

.NET 图形库?