我在php中有一个webapi,运行良好。现在我想在我的应用程序中使用它。 Webapi使用cookie进行身份验证。所以我第一次创建登录任务并从响应中保存cookie。
保存任务:
<form>
<input
type="text"
name="formField"
title="Password must be at least eight characters in length, contain at least one capital letter, one number, and one special character."
pattern="(?=.*[a-z])(?=.*[A-Z])(?=.*[\W_])(?=.*[\d]).{8,}"
required />
<input type="submit" value="Submit" />
</form>
现在我想使用cookies并想要创建获取数据的请求: 数据下载任务:
function testLogin(){
let loginUrlstring = ".../webapi/login.php"
let passwordom = ""
let emailem = ""
guard let url = URL(string: loginUrlstring) else { return }
let postString = "email=" + emailem + "&pw=" + passwordom
var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = "POST"
urlRequest.httpShouldHandleCookies = true
urlRequest.httpBody = postString.data(using: String.Encoding.utf8)
let session = URLSession.shared
let task = session.dataTask(with: urlRequest) { (data, response, error) in
self.saveCookies(response: response)
print("cookies mentve")
print(response?.url)
}
task.resume()}
它说:完成错误 - 代码:-1001 打印数据---没有 我的错误在哪里?
答案 0 :(得分:0)
您应该打印确切的错误,以便了解错误
我的意思是print(error)
let task = session.dataTask(with: urlRequest) { (data, response, error) in ...