如何在 Alamofire 中设置Cookie,以便每次我杀死该应用程序并重新启动它时,都发送相同的Cookie?

最佳答案

Swift 5.1和Alamofire 5.0

准备你的 cookies

let cookieProps = [
    HTTPCookiePropertyKey.domain: "##put your domain here##",
    HTTPCookiePropertyKey.path: "/",
    HTTPCookiePropertyKey.name: "##put your cookie key here##",
    HTTPCookiePropertyKey.value: "##put your cookie value here##"
   ]

设置你的 cookies

if let cookie = HTTPCookie(properties: cookieProps) {
    AF.session.configuration.httpCookieStorage?.setCookie(cookie)
}

之后,您可以执行正常的请求,Cookie将发送到服务器

Alamofire.request(
  ##URL##,
  method: ##.post or .get##,
  parameters: parameters
  ).responseString {
    response in
    ....
}

10-06 02:53