使用模拟器的iOS应用程序报告“此服务器的证书无效”,错误[-1202]错误域=NSURLErrorDomain

3phpmpom  于 2023-03-20  发布在  iOS
关注(0)|答案(1)|浏览(281)

尝试使用iPhone14模拟器向服务器发送http post请求,代码片段如下:

var request = URLRequest(url: URL(string: "https://localhost:8443/process_file"))
  request.httpMethod = "POST"
var body = Data()  // add body data here

let task = URLSession.shared.dataTask(with: request)
{ data, response, error in
    
    // Check for errors
    if let error = error {
        completion(false, error)
        return
    }
    
    // Check for a successful response
    guard let httpResponse = response as? HTTPURLResponse, 
      (200...299).contains(httpResponse.statusCode) else {
        completion(false, nil)
        return
    }
    
    // The request was successful
    completion(true, nil)
  }

   // Start the URLSession task
  task.resume()

我测试了服务器代码使用 dart 客户端发送后的请求,它的作品。使用 Postman 请求也工作。但斯威夫特代码生成以下错误。我试图改变证书没有任何成功。

2023-03-18 16:22:32.467056-0400 MyApp[3511:81183] Connection 1: default TLS Trust 
   evaluation failed(-9807)
2023-03-18 16:22:32.467811-0400 MyApp[3511:81183] Connection 1: TLS Trust encountered 
   error 3:-9807
2023-03-18 16:22:32.468325-0400 MyApp[3511:81183] Connection 1: encountered 
   error(3:-9807)
2023-03-18 16:22:32.484207-0400 MyApp[3511:81183] Task <1C9997DB-4481-42FC-9698- 
   7C1CE90AF535>.<1> HTTP load failed, 0/0 bytes (error code: -1202 [3:-9807])
2023-03-18 16:22:32.524509-0400 MyApp[3511:81183] Task <1C9997DB-4481-42FC-9698- 
  7C1CE90AF535>.<1> finished with error [-1202] Error Domain=NSURLErrorDomain 
  Code=-1202 "The certificate for this server is invalid. You might be connecting to 
  a server that is pretending to be “localhost” which could put your confidential 
  information at risk." UserInfo={NSLocalizedRecoverySuggestion=Would you like to 
  connect to the server anyway?, _kCFStreamErrorDomainKey=3, 
NSErrorPeerCertificateChainKey=("<cert(0x7ff70501f200) s: localhost i: MY-CA>"), 
  NSErrorClientCertificateStateKey=0, 
   NSErrorFailingURLKey=https://localhost:8443/process_file, 
  NSErrorFailingURLStringKey=https://localhost:8443/process_file, 
  NSUnderlyingError=0x60000215d8f0 {Error Domain=kCFErrorDomainCFNetwork Code=-1202 " 
  (null)" UserInfo={_kCFStreamPropertySSLClientCertificateState=0, 
  kCFStreamPropertySSLPeerTrust=<SecTrustRef: 0x600001e35540>, 
 _kCFNetworkCFStreamSSLErrorOriginalValue=-9807, _kCFStreamErrorDomainKey=3, 
 _kCFStreamErrorCodeKey=-9807, kCFStreamPropertySSLPeerCertificates=(
     "<cert(0x7ff70501f200) s: localhost i: MY-CA>"
 )}}, _NSURLErrorRelatedURLSessionTaskErrorKey=(
     "LocalDataTask <1C9997DB-4481-42FC-9698-7C1CE90AF535>.<1>"
   ), _kCFStreamErrorCodeKey=-9807, 
  _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <1C9997DB-4481-42FC-9698- 
  7C1CE90AF535>.<1>, NSURLErrorFailingURLPeerTrustErrorKey=<SecTrustRef: 
  0x600001e35540>, NSLocalizedDescription=The certificate for this server is invalid. 
 You might be connecting to a server that is pretending to be “localhost” which could 
 put your confidential information at risk.}
 Error sending file to server: The certificate for this server is invalid. You might 
  be connecting to a server that is pretending to be “localhost” which could put your 
 confidential information at risk.

在macOS 12.6.3上使用Xcode 14.2

ngynwnxp

ngynwnxp1#

参考Apple的应用传输安全性并添加localhost例外

<key>NSAppTransportSecurity</key>
 <dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>localhost</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSExceptionRequiresForwardSecrecy</key>
            <true/>
            <key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
            <false/>
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
            <true/>
            <key>NSRequiresCertificateTransparency</key>
            <false/>
        </dict>
    </dict>
</dict>

另外记得为开发和生产环境创建单独的Info.plist。或者在上传之前删除这个异常。

相关问题