首页 > 解决方案 > 使用 https 时,它适用于 Android 手机,但在 i-Phone 上收到错误消息:“Error Domain=NSURLErrorDomain Code=-1200”

问题描述

我的视觉工作室 Xamarin.Form 应用程序在 Android 手机中运行良好,但在 iPhone 或 iPhone 模拟器上失败,如下声明:

var content = await _client.GetStringAsync(RI_Url);   // RI_Ur1 is "https://xxxxxxx" 

当我将RI_UrlSafari、Chrome 和 Edge 放入时,它可以显示结果。

如果它有效RI_Uri = "http://xxxxxxx"

我试图将以下内容放在 info.plist 中,但它不起作用。

<key>NSAppTransportSecurity</key>
  <dict>
      <key>NSAllowsArbitraryLoads</key>
      <true/>
  </dict>

有人会帮助解决问题吗?

谢谢。

错误信息是:

{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://xxxxxxxxxxxxxxxxxx, 
NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _kCFStreamErrorDomainKey=3, 
_NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <60343732-0A70-45A6-A4BB-263867134C53>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataTask <60343732-0A70-45A6-A4BB-263867134C53>.<1>"
), NSLocalizedDescription=An SSL error has occurred and a secure connection to the server cannot be made., 
NSErrorFailingURLKey=https://xxxxxxxxxxxxxxxxxxx, 
NSUnderlyingError=0x6000024cafd0 {Error Domain=kCFErrorDomainCFNetwork Code=-1200 "(null)" 
UserInfo={_kCFStreamPropertySSLClientCertificateState=0, _kCFNetworkCFStreamSSLErrorOriginalValue=-9836, _kCFStreamErrorDomainKey=3, 
_kCFStreamErrorCodeKey=-9836}}, _kCFStreamErrorCodeKey=-9836}
   --- End of inner exception stack trace ---
  at System.Net.Http.NSUrlSessionHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) 
[0x001d4] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.2.0.42/src/Xamarin.iOS/Foundation/NSUrlSessionHandler.cs:437 
  at System.Net.Http.HttpClient.SendAsyncWorker (System.Net.Http.HttpRequestMessage request, System.Net.Http.HttpCompletionOption completionOption, 
System.Threading.CancellationToken cancellationToken) [0x0009e] 
in /Users/builder/jenkins/workspace/xamarin-macios/xamarin-macios/external/mono/mcs/class/System.Net.Http/System.Net.Http/HttpClient.cs:281 
  at System.Net.Http.HttpClient.GetStringAsync (System.String requestUri) [0x00033] 
in /Users/builder/jenkins/workspace/xamarin-macios/xamarin-macios/external/mono/mcs/class/System.Net.Http/System.Net.Http/HttpClient.cs:328 
  at xxxxxxxxx.OnGetList () [0x00039] in C:\xxxxxxx\xxxxxxxxPage.xaml.cs:41 }

标签: iphonevisual-studioxamarin.formshttpsxamarin.ios

解决方案


尝试这个:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
    <key>NSExceptionDomains</key>
    <dict>
        <key>yoururl</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
        </dict>
    </dict>
</dict>

推荐阅读