简体   繁体   English

错误域=NSURLErrorDomain 代码=-1202 “此服务器的证书无效

[英]Error Domain=NSURLErrorDomain Code=-1202 "The certificate for this server is invalid

I am trying to call my api in my home network but for some reason, I get the following error message:我试图在我的家庭网络中调用我的 api 但由于某种原因,我收到以下错误消息:

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 “192.168.179.185” which could put your confidential information at risk."完成错误 [-1202] 错误域 = NSURLErrorDomain 代码 = -1202“此服务器的证书无效。您可能正在连接到伪装成“192.168.179.185”的服务器,这可能会使您的机密信息面临风险。 "

I tried some solutions but none of them is fitting my code somehow.我尝试了一些解决方案,但它们都不适合我的代码。

import SwiftUI
import EFQRCode

struct ShowQRCodeView: View {
    //@Binding var isLoggedIn : Bool
    @Binding var deviceId : String
    @Binding var apiKey : String
    @Binding var userId : String
    @Binding var employeeId : Int
    @State private var x = UUID().uuidString
    @State var users = [User]()

var body: some View {
    VStack(){
        Form{
            Section("QR-Code"){
                if let cgImage = EFQRCode.generate(for: deviceId) {
                    Image(uiImage: UIImage(cgImage: cgImage)).resizable().frame(width: 150, height: 150)
                }
                Button("Login"){
                    Task{
                        await doHTTPUserCall()
                    }
                }
            }
        }.frame(height: 180)
        
    }.onAppear {
        if (deviceId == "") {
            deviceId = x // Could change here
        }
        
        
    }
}



func doHTTPUserCall() async {
    
    var url = "https://192.168.179.185:8090/CC0001/BE/admin/api/v1/employee/deviceid/"
    url += String(deviceId)
    guard let reqUrl = URL(string: url) else {
        print("Invalid URL")
        return()
    }
    var req = URLRequest(url: reqUrl)
    req.httpMethod = "GET"
    
    
    let formatter = DateFormatter()
    formatter.dateFormat = "yyyy-MM-dd"
    formatter.timeZone = TimeZone(abbreviation: "ETC")
    
    
    
    let task = URLSession.shared.dataTask(with: req) { data, response, error in
        if let data = data {
            do{
                let decoder = JSONDecoder()
                decoder.dateDecodingStrategy = .formatted(formatter)
                users = try decoder.decode(Array<User>.self, from: data)
                
            } catch{
                print(error)
            }
        } else if let error = error {
            print("HTTP Request Failed \(error)")
        }
        if let response = response as? HTTPURLResponse {
                print("Response HTTP Status code: \(response.statusCode)")
            }
    }
    task.resume()
    
}
 
}

I think it has something to do with a self signed ssl certificate.我认为这与自签名 ssl 证书有关。
Would appreciate any help, thanks将不胜感激任何帮助,谢谢

You can't use a self-signed certificate without first setting up the device to trust the certificate that did the signing (ie, the certificate itself, since it signed itself).如果不先将设备设置为信任进行签名的证书(即证书本身,因为它自己签名),您就不能使用自签名证书。 So you will need to find a way to open that certificate in iOS so that it can be added to the device's trusted certificate store.因此,您需要找到一种方法在 iOS 中打开该证书,以便将其添加到设备的受信任证书存储中。 You could email it to yourself then tap on the attachment, or put it into iCloud Drive and open it from Drive on the phone.您可以自己使用 email 然后点击附件,或将其放入 iCloud Drive 并从手机上的 Drive 打开。 Then got to Settings and search for Trusted Certificates and go there to mark this certificate as trusted.然后进入设置并在那里搜索受信任的证书和 go 以将此证书标记为受信任。

You might find it easier to just get a real cert for your server, such as a free one using certbot.eff.org您可能会发现为您的服务器获取真正的证书更容易,例如使用certbot.eff.org的免费证书

Add this in your info.plist and your issue will be solved.将此添加到您的 info.plist 中,您的问题将得到解决。

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

PS: this issue is occurring because your server on which your api is hosted has no SSL certificate this key will bypass that. PS:出现此问题是因为托管 api 的服务器没有 SSL 证书,此密钥将绕过该证书。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 HTTPS 请求失败,因为 Error Domain=NSURLErrorDomain Code=-1202 仅在 iOS14.6 上 - HTTPS request failed because of Error Domain=NSURLErrorDomain Code=-1202 only on iOS14.6 错误域= NSURLErrorDomain代码= -1004 - Error Domain=NSURLErrorDomain Code=-1004 FAILURE:Error Domain = NSURLErrorDomain Code = -1004“无法连接到服务器。” - FAILURE: Error Domain=NSURLErrorDomain Code=-1004 “Could not connect to the server.” Swift 错误域=NSURLErrorDomain 代码=-999 “已取消” - Swift Error Domain=NSURLErrorDomain Code=-999 “cancelled” 错误域= NSURLErrorDomain代码= -999“已取消” - Error Domain=NSURLErrorDomain Code=-999 “cancelled” 错误域= NSURLErrorDomain代码= -999“已取消” Xcode - Error Domain=NSURLErrorDomain Code=-999 “cancelled” Xcode Alamofire 错误域=NSURLErrorDomain 代码=-999 “已取消” - Alamofire Error Domain=NSURLErrorDomain Code=-999 “cancelled” 域NSURLErrorDomain代码4294966292 - domain NSURLErrorDomain code 4294966292 加载失败,错误为 Error Domain=NSURLErrorDomain Code=-999“已取消” - load failed with error Error Domain=NSURLErrorDomain Code=-999 "cancelled" 如何解决 - 组合错误 | 错误域=NSURLErrorDomain 代码=-999 “已取消” - How to resolve - Combine Error | Error Domain=NSURLErrorDomain Code=-999 “cancelled”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM