我无法在swift中发出POST请求

时间:2015-12-17 06:44:43

标签: ios swift post

我无法在swift中成功发出POST请求。它显示了以下内容 error“kCFStreamErrorCodeKey = -9847 , NSErrorFailingURLStringKey = https://192.168.1.167:8100/Attendance/login,”.

它与我的旧版swift完美配合。现在我已经更新了我的Xcode,发生了这个问题。

func logSetRequest(){
        let urlString = "https://192.168.1.167:8100/Attendance/login"
        let url = NSURL(string: urlString)
        let theRequest = NSMutableURLRequest(URL: url!)

        theRequest.HTTPMethod = "POST"
        let parameters = [ "email": usernameTxtField.text!,"password": passwordTxtField.text!] as Dictionary<String, String>
        var err: NSError?

        do {
            theRequest.HTTPBody = try NSJSONSerialization.dataWithJSONObject(parameters, options: [])
        } catch let error as NSError {
            err = error
            theRequest.HTTPBody = nil
        } // pass dictionary to nsdata object and set it as request body

        theRequest.addValue("application/json", forHTTPHeaderField: "Content-Type")
        theRequest.addValue("application/json", forHTTPHeaderField: "Accept")
        var connectRequest  = NSURLConnection(request: theRequest, delegate: self)
        logActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)

我已经在plist中进行了相关更改

1 个答案:

答案 0 :(得分:0)

从谷歌搜索看来,错误代码-9847似乎与SSLHandSke失败有关。

您说您已升级到Swift的新版本。在iOS 9.0 / OS X 10.11中,Apple引入了App Transport Security,它要求您运行HTTPS,这也是您所需要的。但也许以前有HTTPS设置有问题,但在iOS 9下已停止工作。

您可以在此处详细了解ATS(以及如何暂时禁用它):

http://ste.vn/2015/06/10/configuring-app-transport-security-ios-9-osx-10-11/

希望有所帮助