在Swift 4中发出HTTP POST请求

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

  2018-01-09 20:56:  10.942359 + 0100 EinsatzPush [12182:8624757] [BoringSSL]函数boringssl_session_errorlog:第2871行[boringssl_session_read] SSL_ERROR_ZERO_RETURN(6):操作失败,因为使用close_notify警报干净地关闭了连接
2018-01-09 20:56:10.942857+ 0100 EinsatzPush [12182:8624757] [BoringSSL]函数boringssl_session_errorlog:第2871行[boringssl_session_read] SSL_ERROR_ZERO_RETURN(6):操作失败,因为使用close_notify警报干净地关闭了连接
2018-01-09 20:56:10.943028 + 0100 EinsatzPush [12182:8624757] TIC读状态[1:0x1c0173680]:1:57
2018-01-09 20:56:10.943187 + 0100 EinsatzPush [12182:8624757] TIC读状态[1:0x1c0173680]:1:57
2018 -01-09 20:56:10.943278 + 0100 EinsatzPush [12182:8624757] TIC读状态[1:0x1c0173680]:1:57
2018-01-09 20:56:10.943923 + 0100 EinsatzPush [12182:8624 757] [BoringSSL]函数boringssl_session_errorlog:第2871行[boringssl_session_read] SSL_ERROR_ZERO_RETURN(6):操作失败,因为使用close_notify警报干净地关闭了连接
</ code> </ pre>

这是 我正在使用的代码:</ p>

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

让request = NSMutableURLRequest(url:url as! URL)
request.httpMethod =“POST”

让paramString =“username = user&amp; token = token”
request.httpBody = paramString.data(using:String.Encoding.utf8)

let task = session.dataTask(with:request as URLRequest){

data,response,error)

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

如果让dataString = NSString(data:data!,encoding:String.Encoding.utf8.rawValue)
{
print(dataString) )
}
}

task.resume()
</ code> </ pre>

如果有人在这里帮助我解决此问题,我将非常高兴。 谢谢!</ p>
</ div>

展开原文

原文

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!

doushoubu5360
doushoubu5360 值得注意的是,你将params作为身体而不是params发送。
接近 3 年之前 回复
drll85318
drll85318 我自己能够解决这个问题。我没有将参数放入名为“paramString”的变量中,而是将它们放在实际的url后面,这是在swift变量“url”中定义的。但是非常感谢你试图提供帮助。欣赏它!
接近 3 年之前 回复
donglian6625
donglian6625 所以你正在与服务器沟通。问题出在服务器端,或者您发送的数据不是服务器所期望的。你能不能从后端人那里得到尸体?
接近 3 年之前 回复
douzhan4522
douzhan4522 我得到一个代码200,谷歌称这意味着“好”......这很奇怪
接近 3 年之前 回复
dro80463
dro80463 如果让httpResponse=响应为?HTTPURLResponse{print(“状态代码:(httpResponse.statusCode)”)}
接近 3 年之前 回复
dongweng9474
dongweng9474 我从哪里得到的?
接近 3 年之前 回复
dongshou9343
dongshou9343 这还不足以正确回答我不相信的问题。您至少需要提供状态代码。请求是否超时?
接近 3 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐