加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift – Alamofire请求cookies

发布时间:2020-12-14 05:27:11 所属栏目:百科 来源:网络整理
导读:我是初学者,我无法弄清楚如何使用Alamofire制作.GET请求(但需要进行autenthication).我设法用其他Web服务(登录)执行此操作,因为它需要参数参数: parameters = [ "username" : username"password" : password ] 然后: Alamofire.request(.POST,loginUrl,par
我是初学者,我无法弄清楚如何使用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       
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读