2017-03-08 36 views
1

當我嘗試將我的Swift Mac OS應用連接到在本地主機上的服務器上運行的頁面時,出現此錯誤。我已確保服務器已啓動並正在運行。
這是我的Info.plist文件:NSURLSession/NSURLConnection HTTP加載失敗(kCFStreamErrorDomainSSL,-9843) - Mac OS應用

<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> 

這是試圖訪問該頁面的代碼部分。它來自快速的應用程序。建立我的項目時沒有錯誤。我不能用NSURL因爲這個版本的雨燕沒有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() 
    } 

我曾嘗試更換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}) 

回答

0

是的,我已經嘗試過that.Please看我的info.plist,關鍵NSAppTransportSecurity是.......我找到了解決這個。問題是,我正在使用Mac OS服務器在服務器中運行PHP文件。有趣的是,當我運行localhost/test.php時,它在瀏覽器中工作,但不是從我的swift程序中(它位於同一臺機器上)。我在代碼中使用此鏈接:https://username-macbook-air.local/test.php而不是https://localhost/test.php。好像,當我使用本地主機,在訪問被阻止說:「服務器沒有證書」,即使我包括:

<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> 

所以我想,斯威夫特3在OS X中需要有一個安全的連接SSL證書,這是默認情況下,但我們只需要找到正確的鏈接。

相關問題