如何使用Restkit -objective-c保存和使用cookiea

时间:2022-09-07 08:26:38

I'm using a Rails server and I have an application with Restkit to access the server.

我正在使用Rails服务器,我有一个使用Restkit的应用程序来访问服务器。

My server uses session cookie to login. When I post in /sessions with my account info, I logged in and I can access privates area from server. Question is, I don't want to login whenever I want to use the application. I don't know how restkit works with cookies, but when my session is "open", I don't need to worry about it. But now I want to access private areas, using my cookie.

我的服务器使用会话cookie登录。当我使用我的帐户信息发布/会话时,我登录并且我可以从服务器访问私有区域。问题是,每当我想使用该应用程序时,我都不想登录。我不知道restkit如何使用cookie,但是当我的会话“开放”时,我不需要担心它。但现在我想使用我的cookie访问私有区域。

When I login:

当我登录时:

'POST /sessions HTTP/1.1
 Host: localhost:3000
 Accept-Encoding: gzip, deflate
 Content-Type: application/json
 Content-Length: 74
 Accept-Language: en-us
 Accept: application/json
 Connection: keep-alive
 User-Agent: TicketIn/1.0 CFNetwork/609.1.4 Darwin/11.4.2

 {"session":{"email":"***","password":"***"}}'

 HTTP/1.1 200 OK 
 Content-Type: application/json; charset=utf-8
 X-Ua-Compatible: IE=Edge
 Etag: "7215ee9c7d9dc229d2921a40e899ec5f"
 Cache-Control: max-age=0, private, must-revalidate
 X-Request-Id: 41f0c482720d6cdcedb64071a6ebf9e7
 X-Runtime: 0.222790
 Server: WEBrick/1.3.1 (Ruby/1.9.3/2012-04-20)
 Date: Wed, 26 Jun 2013 13:23:28 GMT
 Content-Length: 1
 Connection: Keep-Alive
 Set-Cookie: _vitrine_session=*session*; path=/; HttpOnly

And when I try to access private area:

当我尝试访问私人区域时:

 GET /ingressos HTTP/1.1
 Host: localhost:3000
 If-None-Match: "310fd8ebf912d0633caa05a91ce56fcc"
 Accept-Encoding: gzip, deflate
 Accept: application/json
 Cookie:      
 _vitrine_session=*session*
 Content-Length: 0
 Accept-Language: en-us
 Connection: keep-alive
 User-Agent: TicketIn/1.0 CFNetwork/609.1.4 Darwin/11.4.2

But when I try to access private area whithout login first:

但是当我尝试首先登录私有区域时:

 GET /ingressos HTTP/1.1
 Host: localhost:3000
 Accept-Encoding: gzip, deflate
 Accept: application/json
 Content-Length: 0
 Connection: keep-alive
 Accept-Language: en-us
 User-Agent: TicketIn/1.0 CFNetwork/609.1.4 Darwin/11.4.2

So How can I include the cookie in my http requests??

那么如何在我的http请求中包含cookie?

Thanks!

谢谢!

1 个解决方案

#1


1  

RestKit is built on top of standard URL loading system. As such, you have complete control over what cookies are used by setting the content of NSHTTPCookieStorage (and if you want dig, changing setHTTPShouldHandleCookies property on NSMutableURLRequest).

RestKit构建于标准URL加载系统之上。因此,您可以通过设置NSHTTPCookieStorage的内容来完全控制使用的cookie(如果您想挖掘,则在NSMutableURLRequest上更改setHTTPShouldHandleCookies属性)。

So, you can add and remove cookies from NSHTTPCookieStorage as and when you require.

因此,您可以根据需要在NSHTTPCookieStorage中添加和删除Cookie。

#1


1  

RestKit is built on top of standard URL loading system. As such, you have complete control over what cookies are used by setting the content of NSHTTPCookieStorage (and if you want dig, changing setHTTPShouldHandleCookies property on NSMutableURLRequest).

RestKit构建于标准URL加载系统之上。因此,您可以通过设置NSHTTPCookieStorage的内容来完全控制使用的cookie(如果您想挖掘,则在NSMutableURLRequest上更改setHTTPShouldHandleCookies属性)。

So, you can add and remove cookies from NSHTTPCookieStorage as and when you require.

因此,您可以根据需要在NSHTTPCookieStorage中添加和删除Cookie。