我正在尝试在 Swift3 中运行 HTTP 请求,将 2 个参数 POST 到 URL。
Example:
Link:
http://test.tranzporthub.com/street45/customer_login.php
Params:
{
"user_id":"[email protected]",
"password":"123"
}
最简单的方法是什么?
我什至不想阅读回复。我只想发送它以通过 PHP 文件对我的数据库执行更改。
我想,你完全是新手,但这里有一些你可以尝试的东西SWIFT 3:
- Open 信息列表文件(双击或右键单击 > 打开为 > 源代码)
-
关闭前粘贴此代码</dict>
and </plist>
tags:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>tranzporthub.com</key>
<dict>
<!--Include to allow subdomains-->
<key>NSIncludesSubdomains</key>
<true/>
<!--Include to allow HTTP requests-->
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
<!--Include to specify minimum TLS version-->
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
</dict>
</dict>
-
现在打开您的视图控制器并将此代码粘贴到您想要发出发布请求的位置:
var request = URLRequest(url: URL(string: "http://test.tranzporthub.com/street45/customer_login.php")!)
request.httpMethod = "POST"
let postString = "[email protected]&password=123"
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(error)")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}
let responseString = String(data: data, encoding: .utf8)
print("responseString = \(responseString)")
}
task.resume()
注意:如果不需要,您可以删除打印语句!
希望这可以帮助! :)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)