Swift 2.0,Alamofire:在HTTP发布请求中设置Cookie

编程入门 行业动态 更新时间:2024-10-27 04:30:50
本文介绍了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

本文发布于:2023-11-26 06:07:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632939.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Swift   Alamofire   Cookie   HTTP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!