Ios HTTP请求后保存并发送cookie
我通过一个API登录,使用带有URLRequest和URLSession的POST请求。自从我收到API的正确响应以来,一切似乎都很好 然后我想发送一个GET请求到同一个API,但似乎我不再连接了 据我所见,API在其标头中以这种形式发送Cookie: Cookie:PHPSESSID=abc123 我猜问题是因为当我执行GET请求时,cookie wich没有发送 这是我的密码 视图控制器:Ios HTTP请求后保存并发送cookie,ios,swift,cookies,Ios,Swift,Cookies,我通过一个API登录,使用带有URLRequest和URLSession的POST请求。自从我收到API的正确响应以来,一切似乎都很好 然后我想发送一个GET请求到同一个API,但似乎我不再连接了 据我所见,API在其标头中以这种形式发送Cookie: Cookie:PHPSESSID=abc123 我猜问题是因为当我执行GET请求时,cookie wich没有发送 这是我的密码 视图控制器: @IBAction func Connection(_ sender: AnyObject)
@IBAction func Connection(_ sender: AnyObject) {
let loginFunc = Login()
loginFunc.login(username: username.text!, password: password.text!) { jsonString in
let response = jsonString
print(response)
}
let get = GetRequest()
get.get(req: "patient/info") { jsonString in
let response = jsonString
print(response)
}
}
Login.swift:
class Login {
func login(username: String, password: String, completion: @escaping (String) -> ()) {
var request = URLRequest(url: URL(string: "http://myurl/web/app_dev.php/login_check")!)
request.httpMethod = "POST"
let config = URLSessionConfiguration.default
let postString = "_username=" + username + "&_password=" + password
request.httpBody = postString.data(using: .utf8)
var responseString = ""
let mysession = URLSession.init(configuration: config)
let task = mysession.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)")
}
responseString = String(data: data, encoding: .utf8)!
let httpResponse = response as! HTTPURLResponse
let field = httpResponse.allHeaderFields["Cookie"]
print(field)
print(type(of: response))
completion(responseString)
}
task.resume()
}
}
GetRequest.swift:
class GetRequest {
func get(req: String, completion: @escaping (String) -> ()) {
var request = URLRequest(url: URL(string: "http://myurl/web/app_dev.php/" + req)!)
request.httpMethod = "GET"
var responseString = ""
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)")
}
responseString = String(data: data, encoding: .utf8)!
print(responseString)
completion(responseString)
}
task.resume()
}
}
嘿,如果要随ajax请求一起发送cookie,请使用。您需要使用withCredentials来设置为true
$.ajaxSetup({
dataType: 'json',
contentType: 'application/json',
async: true,
cache: false,
processData: false,
crossDomain: true,
xhrFields: {
withCredentials: true
}
});
这是swift3而不是ajax?嘿,对不起,你可以使用相同的概念:如果你的应用程序终止,会话cookie将不会被保存为会话cookie。你是什么意思?它没有终止,我正在发送get请求,然后尝试这个会话配置?让config=URLSessionConfiguration.default config.httpCookieAcceptPolicy=.always