在Swift中向服务器发出post请求时出错

时间:2016-01-08 19:56:27

标签: 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); 
?>

1 个答案:

答案 0 :(得分:1)

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

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