代码之家  ›  专栏  ›  技术社区  ›  Amber K

IOS错误代码=-1003–找不到具有指定主机名的服务器。

  •  5
  • Amber K  · 技术社区  · 6 年前

    我以前试过所有的答案,但没有一个对我有用。我可能知道这个问题,我想这个错误是误导性的。 我试图向一个服务器发出api请求,该服务器是https,但没有ssl。而且这些api只能通过vpn工作。所以我禁用了ssl验证,除了一个真正的ios设备之外,它在任何地方(在模拟器、邮递员、android、mac中)都可以正常工作。

    到目前为止,我得到的线索是:

    • DNS污染-我在很多设备上试过很多次,但都不起作用。如果是dns,它在其他android和mac上是如何工作的。
    • vpn隧道-我看到了vpn软件的发布日志这个确切的错误是一个错误,但现在已经修复。我仍然在使用他们的应用,我尝试了我的mac互联网和iphone上的vpn仍然是同样的错误。

    在我看来,这个错误实际上是因为我的代码或者一些逻辑上的东西,需要在ios上完成才能在真正的设备上工作以保证安全性(如果有的话)。

    所以在这里,我将分享到目前为止使它在模拟器上工作的实现(在此之前,我在模拟器上得到了相同的错误)。

    我实现了urlsessiondelegate到我的路由器类,并允许在info.plist中任意加载。所以都是好的url,请求等。

    在实际设备上未调用委托。

     func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
        completionHandler(.useCredential, URLCredential(trust: challenge.protectionSpace.serverTrust!))
    }
    

    提出请求前:

    let config = URLSessionConfiguration.default
    let session = URLSession(configuration: config, delegate: self, delegateQueue: .main)
    

    信息列表文件内容

    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSExceptionDomains</key>
        <dict>
            <key>*my.domain*.com</key>
            <dict>
                <key>NSIncludesSubdomains</key>
                <true/>
                <key>NSExceptionRequiresForwardSecrecy</key>
                <false/>
                <key>NSTemporaryExceptionMinimumTLSVersion</key>
                <string>TLSv1.0</string>
            </dict>
        </dict>
    </dict>
    

    控制台错误:

    [] nw_proxy_resolver_create_parsed_array PAC evaluation error: 
    
    NSURLErrorDomain: -1003
    2018-06-26 20:12:08.646042+0530 MyApp[806:161960] TIC TCP Conn Failed [1:0x1c416f000]: 12:8 Err(-65554)
    2018-06-26 20:12:08.646740+0530 MyApp[806:161964] Task <DCE45907-5758-4CC0-91A1-9EFD53FFDA0A>.<1> HTTP load failed (error code: -1003 [12:8])
    2018-06-26 20:12:08.646971+0530 MyApp[806:161964] Task <DCE45907-5758-4CC0-91A1-9EFD53FFDA0A>.<1> finished with error - code: -1003
    Error Domain=NSURLErrorDomain Code=-1003 "A server with the specified hostname could not be found." UserInfo={NSUnderlyingError=0x1c044cfc0 {Error Domain=kCFErrorDomainCFNetwork Code=-1003 "(null)" UserInfo={_kCFStreamErrorCodeKey=8, _kCFStreamErrorDomainKey=12}}, NSErrorFailingURLStringKey=https://my.domain.com/myurl/public/api, NSErrorFailingURLKey=https://my.domain.com/myurl/public/api, _kCFStreamErrorDomainKey=12, _kCFStreamErrorCodeKey=8, NSLocalizedDescription=A server with the specified hostname could not be found.}
    2018-06-26 20:14:17.727091+0530 MyApp[806:161970] Received XPC error Connection interrupted for message type 3 kCFNetworkAgentXPCMessageTypePACQuery
    2018-06-26 20:14:17.727533+0530 MyApp[806:161970] Received XPC error Connection invalid for message type 3 kCFNetworkAgentXPCMessageTypePACQuery
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Abu Ul Hassan    6 年前

    替换检查

     func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
                completionHandler(.useCredential, URLCredential(trust: challenge.protectionSpace.serverTrust!))
                }
    

    有以下内容

    func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
            if challenge.protectionSpace.authenticationMethod == (NSURLAuthenticationMethodServerTrust) {
                let serverTrust:SecTrust = challenge.protectionSpace.serverTrust!
                var localCertificateTrust = SSLCertificateCreateTrustResult(serverTrust)
                SecTrustEvaluate(serverTrust, &localCertificateTrust)
                if true
                {
                    let credential:URLCredential = URLCredential(trust: serverTrust)
                    challenge.sender?.use(credential, for: challenge)
                    completionHandler(URLSession.AuthChallengeDisposition.useCredential, URLCredential(trust: challenge.protectionSpace.serverTrust!))
    
                } else {
                    completionHandler(URLSession.AuthChallengeDisposition.cancelAuthenticationChallenge, nil)
                }
            }
            else
            {
                completionHandler(URLSession.AuthChallengeDisposition.cancelAuthenticationChallenge, nil);
            }
        }
      fileprivate func SSLCertificateCreateTrustResult(_ serverTrust: SecTrust)->SecTrustResultType {
        let certificate: SecCertificate = SecTrustGetCertificateAtIndex(serverTrust, 0)!
        let remoteCertificateData = CFBridgingRetain(SecCertificateCopyData(certificate))!
        let cerPath: String = Bundle.main.path(forResource: "cert", ofType: "der")!
        let localCertificateData = NSData(contentsOfFile:cerPath)!
        print(localCertificateData.length)
        print((remoteCertificateData as! NSData).length)
        let certDataRef = localCertificateData as CFData
        let cert = (SecCertificateCreateWithData(nil, certDataRef))
        let certArrayRef = [cert] as CFArray
        SecTrustSetAnchorCertificates(serverTrust, certArrayRef)
        SecTrustSetAnchorCertificatesOnly(serverTrust, false)
        let trustResult: SecTrustResultType = SecTrustResultType.invalid
        return trustResult
    }
    

    这里还有我的整个交通安全。

    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
        <key>NSExceptionDomains</key>
        <dict>
            <key>dev-domainserver/portal</key>
            <dict>
                <key>NSExceptionMinimumTLSVersion</key>
                <string>TLSv1.2</string>
                <key>NSExceptionRequiresForwardSecrecy</key>
                <false/>
                <key>NSExceptionAllowsInsecureHTTPLoads</key>
                <true/>
            </dict>
        </dict>
    </dict>