Android模拟器中MAUI调用本地C# API抛出HttpRequestException求助
问题:MAUI Android模拟器调用本地自签名证书API失败
我的MAUI应用和C# API项目在同一解决方案中,Windows上运行正常,但在VS Android模拟器中调用API时抛出以下异常:
System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception. ---> System.Security.Authentication.AuthenticationException: Authentication failed, see inner exception. ---> Interop+AndroidCrypto+SslException: Exception of type 'Interop+AndroidCrypto+SslException' was thrown. --- End of inner exception stack trace ---
已知这是Android不信任本地开发机自签名证书的问题,我尝试了以下方案但无效:
添加
Platforms/Android/Resources/xml/network_security_config.xml配置文件,设置调试环境忽略证书验证:<?xml version="1.0" encoding="utf-8"?> <network-security-config> <!-- 允许本地开发时向10.0.2.2发送明文流量(如需) --> <domain-config cleartextTrafficPermitted="true"> <domain includeSubdomains="false">10.0.2.2</domain> </domain-config> <!-- 允许应用信任用户安装的证书(仅调试环境) --> <debug-overrides> <trust-anchors> <certificates src="user" /> <certificates src="system" /> </trust-anchors> </debug-overrides> </network-security-config>在
AndroidManifest.xml中引用上述配置文件:<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.companyname.moviemanager"> <application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true" android:networkSecurityConfig="@xml/network_security_config"></application> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> </manifest>将本地自签名证书导出为.pfx文件并安装到Android模拟器,甚至通过以下PowerShell命令重新生成证书后安装:
$cert = New-SelfSignedCertificate -Subject "CN=DevCertificate" -CertStoreLocation "Cert:\CurrentUser\My"
恳请指点下一步排查方向。
内容的提问来源于stack exchange,提问作者USMC6072
相关产品推荐
相关产品推荐

