本文介绍了Swift 2.0,Alamofire:在HTTP发布请求中设置Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在HTTP POST请求中设置cookie。
I want to set cookies in my HTTP POST request.
类似于下面HTTP请求中的cookie字段,
Something like the cookie field in the HTTP Request below,
version: 0.1.7 Cookie: client=Android; version=0.1.7; sellerId=SEL5483318784; key=178a0506-0639-4659-9495-67e5dffa42de Content-Type: application/x-www-form-urlencoded; charset=UTF-8 Content-Length: 1431如何使用Alamofire做到这一点?
How can I achieve this with Alamofire?
我当前的Alamofire请求就是这样,
My current Alamofire request is like this,
Alamofire.request(.POST, ServerConfig.ADD_PRODUCT_URL, parameters: productJSON, encoding: .JSON, headers: nil) .responseJSON(completionHandler: { responseRequest, responseResponse, responseResult in print(responseRequest!.URL) print(responseResponse) print(responseResult) })推荐答案
我在一个项目上遇到了同样的问题,我做了这样的事情来解决它:
I had the same problem on a project and I do something like this to solve it:
let cookies = NSHTTPCookie.cookiesWithResponseHeaderFields(response.allHeaderFields as! [String: String], forURL: response.URL!) Alamofire.Manager.sharedInstance.session.configuration.HTTPCookieStorage?.setCookies(cookies, forURL: response.URL!, mainDocumentURL: nil)您只需执行一次,因为Alamofire实例是单例,因此对于所有下一个请求都设置了cookie。
You just have to do this one time, because the Alamofire instance is a singleton, so for all the next request the cookie is set.
希望这就是您想要的东西:)
Hope it's what you are looking for :)
更多推荐
Swift 2.0,Alamofire:在HTTP发布请求中设置Cookie
发布评论