在Swift 4中发出HTTP POST请求

时间:2018-01-09 20:04:51

标签: php swift http post swift4

我正在尝试使用URLSession将数据发送到我服务器上的PHP文件。但这似乎失败了,因为服务器上没有收到任何数据。 在运行应用程序时,我得到了这个控制台打印:

2018-01-09 20:56:10.942359+0100 EinsatzPush[12182:8624757] [BoringSSL] Function boringssl_session_errorlog: line 2871 [boringssl_session_read] SSL_ERROR_ZERO_RETURN(6): operation failed because the connection was cleanly shut down with a close_notify alert
2018-01-09 20:56:10.942857+0100 EinsatzPush[12182:8624757] [BoringSSL] Function boringssl_session_errorlog: line 2871 [boringssl_session_read] SSL_ERROR_ZERO_RETURN(6): operation failed because the connection was cleanly shut down with a close_notify alert
2018-01-09 20:56:10.943028+0100 EinsatzPush[12182:8624757] TIC Read Status [1:0x1c0173680]: 1:57
2018-01-09 20:56:10.943187+0100 EinsatzPush[12182:8624757] TIC Read Status [1:0x1c0173680]: 1:57
2018-01-09 20:56:10.943278+0100 EinsatzPush[12182:8624757] TIC Read Status [1:0x1c0173680]: 1:57
2018-01-09 20:56:10.943923+0100 EinsatzPush[12182:8624757] [BoringSSL] Function boringssl_session_errorlog: line 2871 [boringssl_session_read] SSL_ERROR_ZERO_RETURN(6): operation failed because the connection was cleanly shut down with a close_notify alert

这是我使用的代码:

let url = URL(string: "https://my-server.com/update.php")
    let session = URLSession.shared

    let request = NSMutableURLRequest(url: url as! URL)
    request.httpMethod = "POST"

    let paramString = "username=user&token=token"
    request.httpBody = paramString.data(using: String.Encoding.utf8)

    let task = session.dataTask(with: request as URLRequest) {
        (
        data, response, error) in

        guard let _:NSData = data as NSData?, let _:URLResponse = response, error == nil else {
            print("error")
            return
        }

        if let dataString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
        {
            print(dataString)
        }
    }

    task.resume()
如果有人能帮助我解决这个问题,我将非常高兴。 谢谢!

0 个答案:

没有答案