php - 在 Swift 中向服务器发出发布请求时出错

标签 php ios swift

我对 iOS 开发还比较陌生。目前,我正在关注 tutorial在 Swift 中向服务器发出 POST 请求。但是,我收到错误消息,我不太明白它出了什么问题。

    2016-01-08 14:44:48.991 test[24331:4311765] NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)
error=Optional(Error Domain=NSURLErrorDomain Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made." UserInfo={NSLocalizedDescription=An SSL error has occurred and a secure connection to the server cannot be made., NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _kCFStreamErrorDomainKey=3, NSUnderlyingError=0x7c28c9b0 {Error Domain=kCFErrorDomainCFNetwork Code=-1200 "(null)" UserInfo={_kCFStreamPropertySSLClientCertificateState=0, _kCFNetworkCFStreamSSLErrorOriginalValue=-9802, _kCFStreamErrorCodeKey=-9802, _kCFStreamErrorDomainKey=3, kCFStreamPropertySSLPeerTrust=<SecTrustRef: 0x7ae43c60>, kCFStreamPropertySSLPeerCertificates=<CFArray 0x7c28aad0 [0x4ef098]>{type = immutable, count = 1, values = (
    0 : <cert(0x7c191330) s: localhost i: localhost>
)}}}, _kCFStreamErrorCodeKey=-9802, NSErrorFailingURLStringKey=https://localhost/, NSErrorPeerCertificateChainKey=<CFArray 0x7c28aad0 [0x4ef098]>{type = immutable, count = 1, values = (
    0 : <cert(0x7c191330) s: localhost i: localhost>
)}, NSErrorClientCertificateStateKey=0, NSURLErrorFailingURLPeerTrustErrorKey=<SecTrustRef: 0x7ae43c60>, NSErrorFailingURLKey=https://localhost/})

Swift 中的 POST 请求:

let myUrl = NSURL(string: "https://localhost");
let request = NSMutableURLRequest(URL:myUrl!);
request.HTTPMethod = "POST";
// Compose a query string
let postString = "firstName=James&lastName=Bond";

request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);

let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
            data, response, error in

            if error != nil
            {
                print("error=\(error)")
                return
            }

            // You can print out response object
            print("response = \(response)")

            // Print out response body
            let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
            print("responseString = \(responseString)")

            //Let’s convert response sent from a server side script to a NSDictionary object:

            do {
                let myJSON = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary

                // YOUR CODE HERE
                if let parseJSON = myJSON {
                    // Now we can access value of First Name by its key
                    let firstNameValue = parseJSON["firstName"] as? String
                    print("firstNameValue: \(firstNameValue)")
                }

            } catch {
                print(error)
            }
 }

 task.resume()

index.php 中的代码:

<?php
// Read request parameters
 $firstName= $_REQUEST["firstName"];
 $lastName = $_REQUEST["lastName"];// Store values in an array
 $returnValue = array(“firstName”=>$firstName, “lastName”=>$lastName);
// Send back request in JSON format
 echo json_encode($returnValue); 
?>

最佳答案

您的本地主机不是 https,而是 http。在 iOS 9 中,您必须在 info.plist 中禁用它,以便它允许您向非 https 目标发出请求。

将此添加到您的 info.plist enter image description here

关于php - 在 Swift 中向服务器发出发布请求时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34685058/

相关文章:

ios - 如何使用 RxSwift 有条件地观察和绑定(bind)

javascript - jQuery Ajax POST JSON 问题

ios - 如果条件为真,如何转移到特定的 Storyboard?

php - 在 WooCommerce 的主页上隐藏 "out of stock"产品

iphone - 如何将 unix 时间戳转换为人类可读时间?

ios - 如何将自定义类添加到我的 UITableViewController?

ios - 如果我续订我的服务器证书并且我的应用程序正在进行证书固定,会发生什么情况?

ios - 在过渡期间去除阴影和透明度

php - 如何使用适用于 Yii2 的 AWS SDK 将图像上传到数字 Ocean Spaces?

php - 在 while 循环中执行函数