这可能是基本问题,但我无法调查此问题。如果有人遇到它,需要你的帮助。
在创建NSURLRequest期间,我将handle cookie属性设置为true。
urlRequest.HTTPShouldHandleCookies=true
当登录响应到来时,我们正在保存cookie;请看一下代码:
//Stored the cookies in shared place
let allHeaderFileds = httpResponse.allHeaderFields as! [String : String]
let cookies: NSArray? = NSHTTPCookie.cookiesWithResponseHeaderFields(allHeaderFileds, forURL: (response?.URL)!)
if let _ = cookies
{
NSHTTPCookieStorage.sharedHTTPCookieStorage().setCookies(cookies! as! [NSHTTPCookie], forURL: (response?.URL)!, mainDocumentURL: nil)
}
当我们进行另一个我们需要这些cookie的API调用时,我们也在这里设置如下:
let cookieProperties:NSDictionary = [
NSHTTPCookieName:cookie.name!,
NSHTTPCookieValue:cookie.value!,
NSHTTPCookieDomain:cookie.domain!,
NSHTTPCookiePath:cookie.path!
]
let cookie:NSHTTPCookie = NSHTTPCookie(properties: cookieProperties as! [String : AnyObject])!
NSHTTPCookieStorage.sharedHTTPCookieStorage().setCookie(cookie)
当我点击API调用以获取数据时,它返回状态代码403和消息“未提供身份验证凭据。”
有关标题的更多详细信息,请查看以下内容:
"<NSHTTPURLResponse: 0x7fb5227444a0> { URL: http://172.16.10.249:8001/api/v1/users/profile/ } { status code: 403, headers {\n Allow = \"GET, POST, HEAD, OPTIONS\";\n \"Content-Type\" = \"application/json\";\n Date = \"Thu, 28 Jan 2016 10:22:22 GMT\";\n Server = \"WSGIServer/0.1 Python/2.7.11\";\n Vary = \"Accept, Cookie\";\n \"X-Frame-Options\" = SAMEORIGIN;\n} }"
如果我错在任何一点或者我们需要在iOS9上进行一些额外的配置,请告诉我。如果服务器需要做更多的步骤,请同时提及它。我们的服务器是用Python编写的。