swift – Alamofire请求cookies

前端之家收集整理的这篇文章主要介绍了swift – Alamofire请求cookies前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是初学者,我无法弄清楚如何使用Alamofire制作.GET请求(但需要进行autenthication).我设法用其他Web服务(登录)执行此操作,因为它需要参数参数:
parameters = [
 "username" : username
"password" : password 
]

然后:

Alamofire.request(.POST,loginUrl,parameters: parameters).responseJSON { (request,response,data,error) -> Void in

     //handling the response       
}

回复标题中我得到一些信息:

[Transfer-Encoding: Identity,Server: Nginx/1.4.1,Content-Type: application/json,P3P: policyref="http://www.somewebpage.com",CP="NON DSP COR CURa TIA",Connection: keep-alive,Date: Sun,08 Mar 2015 13:49:20 GMT,Vary: Accept-Encoding,Cookie,Set-Cookie: sessionid=5xeff47e65f674a4cc5b2d54f344304b; Domain=.somedomain.com; Path=/,tbauth=1; Domain=.somedomain.com; Path=/,Content-Encoding: gzip]

它的类型为[NSObject:AnyObject]

如何将该信息存储在NSURLDefaults中并准备有效的请求参数(cookie)?我需要所有字段还是只需要Set-Cookie?

我试过手动设置参数:

parameters = [
 "Cookie" : "sessionid=5xeff47e65f674a4cc5b2d54f344304b; Domain=.somedomain.com; Path=/,tbauth=1; Domain=.somedomain.com; Path=/"
]

但它确实返回错误NSURLErrorDomain -1017(NSURLErrorCannotParseResponse)

感谢所有回复.

好的,2周后我才找到解决方案:
let URL = NSURL(string: query)!
let mutableUrlRequest = NSMutableURLRequest(URL: URL)
mutableUrlRequest.HTTPMethod = "GET"

let prefs = NSUserDefaults.standardUserDefaults()
let cookie = prefs.valueForKey("COOKIE") as String

mutableUrlRequest.setValue(cookie,forHTTPHeaderField: "Cookie")

Alamofire.request(mutableUrlRequest).responseJSON { (request,error) -> Void in

     //handling the response       
}
原文链接:https://www.f2er.com/swift/319233.html

猜你在找的Swift相关文章