我查了一下如何发送请求,并在这里找到答案,但虽然它似乎适用于其他所有人,但它不适合我。可以请一些请帮助我获取实际发送POST请求的代码,并打印响应。 URL工作,因为我可以通过我的borwser手动输入信息,但它是一个局域网网站,但这个应用程序仅用于学习SWIFT。
SELECT date FROM articles WHERE MATCH (description) AGAINST ('+"big" +"light"' IN BOOLEAN MODE);
} `
我的问题正在这里发生
import UIKit
class PostController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func sendPost(url:String,attrs:String) {
print("YO BOY")
var request = URLRequest(url: URL(string: url)!)
request.httpMethod = "POST"
let postString = attrs
request.httpBody = postString.data(using: .utf8)
print("NABBBADABBA")
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=\(String(describing: error))")
return
}
print("CHIKKA CHIKKA")
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(String(describing: response))")
}
print("FLEKKA FLEEKA WAHAPPA")
let responseString = String(data: data, encoding: .utf8)
print("responseString = \(String(describing: responseString))")
}
task.resume()
}
@IBOutlet var text: UITextView!
@IBAction func post(_ sender: Any) {
print("SENDING POST")
sendPost(url: "localhost/testingtest.php",attrs: "post=\(text.text)")
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
终端说:
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=\(String(describing: error))")
return
}