Jim*_*mmy 5 .net android maui .net-maui
我在这里下载了最新的 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
Run Code Online (Sandbox Code Playgroud)
我HttpClient通过工厂方法在 MauiProgram.cs 中初始化AddHttpClient,如下所示(注意 Andorid 的不同 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); });
Run Code Online (Sandbox Code Playgroud)
android:networkSecurityConfig我已在 AndroidManifest.xml 中包含以下内容(允许 http 流量):
<network-security-config>
<base-config cleartextTrafficPermitted="true">
<trust-anchors>
<certificates src="system" />
</trust-anchors>
</base-config>
</network-security-config>
Run Code Online (Sandbox Code Playgroud)
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.
Run Code Online (Sandbox Code Playgroud)
我也尝试过这个
handler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;
Run Code Online (Sandbox Code Playgroud)
但出现错误
System.PlatformNotSupportedException: Operation is not supported on this platform
Run Code Online (Sandbox Code Playgroud)
已经连续 4 天了,我已经检查了工作解决方案中所有可以想象的设置,但找不到任何差异。有任何想法吗?
@Jimmy 在你的代码中,当你在 android 上运行时,你将 URL 从 更改"https://localhost" 为 。在这两种情况下,您都在端口7096"http://10.0.2.2"调用 API :。
对于 android,您使用的是 HTTP 协议,而在所有其他情况下,您都使用 HTTPS。public static string APIUrl = $"{Base}:7096/api/";
我假设您的服务器仅在端口 7096 上侦听 HTTP S,而不侦听 HTTP。也许这会导致您的问题。
另一个问题可能是您的服务仅绑定到localhost您的 IP 地址,而不绑定到您的 IP 地址(您可以在 API 的 launchsettings.json 中查找这一点)。
另一个问题可能是您的防火墙不允许端口 7096 上的传入流量。您也应该检查这一点,因为当您在 Windows 上运行时,您不会跨越计算机边界。但是,在 Android 模拟器上运行时,您可以进行跨机器通信。
| 归档时间: |
|
| 查看次数: |
9181 次 |
| 最近记录: |