doumeilmikv7099 2018-01-09 20:04
浏览 652

在Swift 4中发出HTTP POST请求

I´m trying to send data to a PHP file on my server using URLSession. This seems to fail though, since no data is received on the server. While running the app, I get this console print:

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

This is the code I´m using:

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

I´d be very happy if anyone here could help me get this fixed. Thanks!

  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥15 链接问题 C++LNK2001 无法解析的外部符号
    • ¥50 安装pyaudiokits失败
    • ¥15 计组这些题应该咋做呀
    • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
    • ¥15 让node服务器有自动加载文件的功能
    • ¥15 jmeter脚本回放有的是对的有的是错的
    • ¥15 r语言蛋白组学相关问题
    • ¥15 Python时间序列如何拟合疏系数模型
    • ¥15 求学软件的前人们指明方向🥺
    • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接