Alamofire请求与Cookie [英] Alamofire request with cookies

查看:1945
本文介绍了Alamofire请求与Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是初学者,我不知道如何使用Alamofire来创建.GET请求(但它需要验证)。我设法使用其他web服务(登录),因为它需要参数参数:

I'm beginner and I can't figure out how to make a .GET request (however it requires autenthication) with Alamofire. I managed to do this with other web service (login) because it takes parameters argument:

parameters = [
 "username" : username
"password" : password 
]

/ p>

Then:

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

     //handling the response       
}

响应头我得到一些信息:

In response header I get some information:

[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]

:AnyObject]

我应该如何处理这些信息,将其存储在NSURLDefaults并准备有效的请求参数(cookie)?

What should I do with that information to store it in NSURLDefaults and prepare valid request parameter (cookie)? Do I need all fields or just Set-Cookie?

我尝试手动设置参数:

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

但返回错误NSURLErrorDomain -1017(NSURLErrorCannotParseResponse)

but it does return error NSURLErrorDomain -1017 (NSURLErrorCannotParseResponse)

感谢所有回复。

推荐答案

/ p>

Ok, after 2 weeks I just found a solution:

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, response, data, error) -> Void in

     //handling the response       
}

这篇关于Alamofire请求与Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆