cookie的俩种获取方法:
即根据URLSession请求返回信息后,根据请求头中的Set-Cookie获取Cookie
//urlRequest。。。。
let task:URLSessionDataTask = URLSession.shared.dataTask(with: urlRequest, completionHandler: { (data, response, error) in
guard error == nil else {
resultBlock(nil, error)
print("读取数据 error !",error ?? "")
return
}
//方法1:
let cookies = HTTPCookie.cookies(withResponseHeaderFields: (response as! HTTPURLResponse).allHeaderFields as! [String : String], for: uRL!)
//方法2:
let cookieString = (response as! HTTPURLResponse).allHeaderFields["Set-Cookie"]
HTTPCookieStorage.shared.setCookie(cookies)
let (object,convertError) = T.convertFromData(data: data as NSData!)
resultBlock(object as? T,convertError)
})
网友评论