【发布时间】:2020-07-04 12:51:20
【问题描述】:
我有一个连接到 API(由我编写)的 Xamarin iOS 应用程序。有时我会遇到模拟器(在 Windows 10 上运行)无法连接到 API 的问题。而相同的代码在连接到我网络上的 MAC 的 iPhone 上运行良好。
它会抛出以下异常:
{System.Net.Http.HttpRequestException: An SSL error has occurred and a secure connection to the server cannot be made.
---> Foundation.NSErrorException: Error Domain=NSURLErrorDomain Code=-1200
"An SSL error has occurred and a secure connection to the server cannot be made."
UserInfo={NSErrorFailingURLStringKey=https://api.staging.MyApp.com/api/v1/LogOn/,
NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?,
_kCFStreamErrorDomainKey=3,
_NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <61E93B00-972D-4DF1-BA39-0141116988C2>.<1>,
_NSURLErrorRelatedURLSessionTaskErrorKey=(\n "LocalDataTask <61E93B00-972D-4DF1-BA39-0141116988C2>.<1>"\n),
NSLocalizedDescription=An SSL error has occurred and a secure connection to the server cannot be made.,
NSErrorFailingURLKey=https://api.staging.MyApp.com/api/v1/LogOn/,
NSUnderlyingError=0x600001cac840 {Error Domain=kCFErrorDomainCFNetwork Code=-1200 "(null)" UserInfo={_kCFStreamPropertySSLClientCertificateState=0,
_kCFNetworkCFStreamSSLErrorOriginalValue=-9816,
_kCFStreamErrorDomainKey=3,
_kCFStreamErrorCodeKey=-9816}},
_kCFStreamErrorCodeKey=-9816}\n
--- End of inner exception stack trace ---\n
at System.Net.Http.NSUrlSessionHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x001d4] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.18.2.1/src/Xamarin.iOS/Foundation/NSUrlSessionHandler.cs:523 \n
at System.Net.Http.HttpClient.FinishSendAsyncBuffered (System.Threading.Tasks.Task`1[TResult] sendTask,
System.Net.Http.HttpRequestMessage request, System.Threading.CancellationTokenSource cts,
System.Boolean disposeCts) [0x0017e] in
/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/external/corefx/src/System.Net.Http/src/System/Net/Http/HttpClient.cs:506 \n at MyApp.Mobile.Api.LogOnUser (MyApp.Mobile.VM.LogOnVM vm) [0x000b2] in D:\Projects\MyAppCore\MyApp.Mobile\MyApp.Mobile\Api.cs:114 }
重复一遍,VERY SAME 代码完美地在物理 iphone(和 android 模拟器/手机)上运行。
任何关于为什么的想法都值得赞赏。谢谢。
【问题讨论】:
-
可能是一个证书问题,它与设备上受信任的证书存储区与 mac/simulator 的差异相冲突。根据您在 api 上使用的证书类型,您可能需要研究如何在模拟器上以受信任的方式安装它和/或查看在实际证书验证过程发生时挂钩并调试它。
-
iOS 模拟器不在 Windows 10 上运行 - 它始终在 Mac 上运行。它可能在 Windows 上显示,但在 Mac 上运行。
-
将重新启动 MAC(再次;)
-
修复了它。 Jason,你想创建一个答案吗 - 尝试重新启动 MAC,我会将其标记为已修复。
标签: xamarin xamarin.forms xamarin.ios