iPhone Make POST request, handle cookie

后端 未结 2 1833
栀梦
栀梦 2020-12-23 18:27

I\'m hoping someone can shed some light on the following, I think I am heading in the right direction with this. I want to login to my server with a user/pass combo, then I

相关标签:
2条回答
  • 2020-12-23 18:42

    This should work:

    NSDictionary *headerFields = [(NSHTTPURLResponse*)response allHeaderFields]; 
    NSURL *url = [NSURL URLWithString:@"http://www.mywebserver.com/login.php"];   
    NSArray *cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:headerFields forURL:url];
    

    Then you can know if the cookies array contains the cookie you want.

    You could also call this after receiving the response:

    NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:url];
    
    0 讨论(0)
  • 2020-12-23 18:59

    Here is a possible solution using your current code:

    1 - What you do at the beginning is good: setting the cookie policy generally.

    NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHttpCookieStorage];
    [cookieStorage setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
    

    2 - After that, in you requests, you need to specify that you want to use cookies (in order to inject the session information):

    [request setHTTPShouldHandleCookies:YES];
    

    You need to set this field for the authentication and the subsequent requests.

    0 讨论(0)
提交回复
热议问题