我在这里下载了最新的示例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我正在通过类似的HttpClient工厂方法在MauiProgram.cs中初始化AddHttpClient (注意安道尔的不同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); });我在android:networkSecurityConfig中包含了以下AndroidManifest.xml (它允许http流量):
<network-security-config>
<base-config cleartextTrafficPermitted="true">
<trust-anchors>
<certificates src="system" />
</trust-anchors>
</base-config>
</network-security-config>Xamarin中的解决方案似乎是使用AndroidClientHandler,但这是一个我不能在MAUI中使用的不推荐的库,并且在工作的链接解决方案中没有引用它。
我尝试过的事物:
使用Https:我在这里遵循了指南:https://learn.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天了,我已经经历了所有可以想象到的解决方案,却找不到任何不同之处。有什么想法吗?
发布于 2022-05-25 15:00:53
@Jimmy在您的代码中,当您在android上运行时,您将从"https://localhost"更改为"http://10.0.2.2"。在这两种情况下,您都是在端口7096:public static string APIUrl = $"{Base}:7096/api/";上调用API。对于android,您使用的是HTTP协议,在所有其他情况下,您使用的是HTTPS。
我假设您的服务器只在端口7096上侦听HTTPS,而不监听HTTP。也许这会引起你的问题。
另一个问题可能是您的服务仅绑定到localhost,而不是绑定到您的IP地址(您可以在API的launchsettings.json中查找这个问题)。
另一个问题可能是防火墙不允许端口7096上的传入通信量。您也应该检查这一点,因为在Windows上运行时,您没有跨越机器边界。但是,在Android仿真程序上运行时,您可以进行跨机通信。
https://stackoverflow.com/questions/72313635
复制相似问题