【问题标题】:NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9843) - Mac OS appNSURLSession/NSURLConnection HTTP 加载失败 (kCFStreamErrorDomainSSL, -9843) - Mac OS 应用
【发布时间】:2017-03-08 04:49:22
【问题描述】:

当我尝试将我的 Swift Mac OS 应用程序连接到在我的本地主机上的服务器上运行的页面时,我收到此错误。我已确保服务器已启动并正在运行。
这是我的 info.plist 文件:

<plist version="1.0">
<dict>
    <key>UIBackgroundModes</key>
    <array>
        <string></string>
    </array>
    <key>NSLocationAlwaysUsageDescription</key>
    <string>Location Service always in use</string>
    <key>UIRequiredDeviceCapabilities</key>
    <array>
        <string></string>
    </array>
    <key>CFBundleIdentifier</key>
    <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>LSApplicationCategoryType</key>
    <string></string>
    <key>CFBundleShortVersionString</key>
    <string>1.0</string>
    <key>CFBundleExecutable</key>
    <string>$(EXECUTABLE_NAME)</string>
    <key>CFBundleVersion</key>
    <string>1</string>
    <key>NSMainStoryboardFile</key>
    <string>Main</string>
    <key>NSPrincipalClass</key>
    <string>NSApplication</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>NSLocationWhenInUseDescrciption</key>
    <string>Location Needed</string>
    <key>CFBundleIconFile</key>
    <string></string>
    <key>LSMinimumSystemVersion</key>
    <string>$(MACOSX_DEPLOYMENT_TARGET)</string>
    <key>CFBundleDevelopmentRegion</key>
    <string>en</string>
    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSExceptionDomains</key>
        <dict>
            <key>17.83.148.252</key>
            <dict>
                <key>NSIncludesSubdomains</key>
                <true/>
                <key>NSExceptionRequiresForwardSecrecy</key>
                <false/>
                <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
                <true/>
                <key>NSTemporaryExceptionMinimumTLSVersion</key>
                <string>TLSv1.1</string>
            </dict>
        </dict>
    </dict>
    <key>NSHumanReadableCopyright</key>
    <string>Copyright © 2017 Meghalee. All rights reserved.</string>
    <key>CFBundleName</key>
    <string>$(PRODUCT_NAME)</string>
    <key>NSAllowsArbitraryLoads</key>
    <string>YES</string>
    <key>NSExceptionAllowsInsecureHTTPLoads</key>
    <string></string>
</dict>
</plist>

这是尝试访问页面的代码部分。它来自 swift 应用程序。构建项目时没有错误。我不能使用 NSURL,因为这个版本的 Swift 没有 NSUrl,它被更改为 URLSession、URLRequest 等等:

 private func sendtoPHP(nSt : NetworkStatistics,lat : Double, long : Double )
    {
        var request = URLRequest(url: URL(string: "https://17.83.148.252/test.php")!)
        request.httpMethod = "POST"

//        let postString = "a=\(nSt.getCurrentSsid()!)&b=\(nSt.getRssiValue()!)&c=\(nSt.getNoiseMeasurement()!)&d=\(nSt.getWlanChannel()!)&e=\(nSt.getBssid()!)&f=\(nSt.getCountryCode()!)&g=\(nSt.getHardwareAddress()!)&h=\(nSt.getTransmitPower()!)&i=\(lat)&j=\(long)"

        let postString = "a=\(nSt.getCurrentSsid()!)"
        request.httpBody = postString.data(using: .utf8)
        let task = URLSession.shared.dataTask(with: request) { data, response, error in
            guard let data = data, error == nil else {                                                 // check for fundamental networking error
                print("error=\(error)")
                return
            }
            if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {           // check for http errors
                print("statusCode should be 200, but is \(httpStatus.statusCode)")
                print("response = \(response)")
            }

            let responseString = String(data: data, encoding: .utf8)
            print("responseString = \(responseString)")
        }
        task.resume()
    }

我尝试用 localhost 替换 17.83.148.252 但它不起作用。
以下是详细的错误描述:

2017-03-08 11:53:40.453196 NetworkHealth_Mac[56146:753522] Unfiltered exception: SSLHostname
2017-03-08 11:53:40.479160 NetworkHealth_Mac[56146:753489] NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9843)
error=Optional(Error Domain=NSURLErrorDomain Code=-1202 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “17.83.148.252” which could put your confidential information at risk." UserInfo={NSURLErrorFailingURLPeerTrustErrorKey= NSLocalizedDescription=The certificate for this server is invalid. You might be connecting to a server that is pretending to be “17.83.148.252” which could put your confidential information at risk., NSErrorFailingURLKey=https://17.83.148.252/test.php, NSErrorFailingURLStringKey=https://17.83.148.252/test.php, NSErrorClientCertificateStateKey=0})

【问题讨论】:

    标签: swift


    【解决方案1】:

    是的,我已经尝试过了。请查看我的 info.plist,密钥 NSAppTransportSecurity 在那里。我找到了解决方案。问题是,我使用 Mac OS 服务器在服务器中运行 PHP 文件。有趣的是,当我运行 localhost/test.php 时,它在浏览器中运行,但不是从我的 swift 程序(在同一台机器上)中运行。我在代码中使用了这个链接:https://username-macbook-air.local/test.php 而不是https://localhost/test.php。好像,当我使用 localhost 时,访问被阻止说“服务器没有证书”,即使我包括:

    <key>NSAppTransportSecurity</key>
        <dict>
            <key>NSExceptionDomains</key>
            <dict>
                <key>localhost</key>
                <dict>
                    <key>NSIncludesSubdomains</key>
                    <true/>
                    <key>NSExceptionRequiresForwardSecrecy</key>
                    <false/>
                    <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
                    <true/>
                    <key>NSTemporaryExceptionMinimumTLSVersion</key>
                    <string>TLSv1.1</string>
                </dict>
            </dict>
        </dict>
    

    所以我认为 OS X 中的 Swift 3 需要与 SSL 证书的安全连接,默认情况下存在 SSL 证书,但我们只需要找到正确的链接。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-06
      • 2016-04-24
      • 2015-09-04
      • 1970-01-01
      • 2016-10-19
      • 2016-01-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多