如何获取cookie并将其用于其他请求,如POST (iOS)?

时间:2021-09-25 13:42:13

My previous question was about the problem that I have to login each time for doing web services like posting a link or uploading a picture. Philipe answered that I have to use cookies instead of login process for each request. I found this method for getting cookies:

我之前的问题是,每次做web服务时都要登录,比如发布链接或上传图片。Philipe回答说,我必须使用cookie而不是每个请求的登录过程。我发现了获取饼干的方法:

- (void)getCookies {

    NSHTTPURLResponse * response;
    NSError * error;
    NSMutableURLRequest *request;

    request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://MyWebsite.com/login.php"]
                                            cachePolicy:NSURLRequestReloadIgnoringCacheData
                                        timeoutInterval:120];
    NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]);
    NSArray * all = [NSHTTPCookie cookiesWithResponseHeaderFields:[response allHeaderFields] forURL:[NSURL URLWithString:@"http://MyWebsite.com/login.php"]];
    NSLog(@"%d", all.count);

    for (NSHTTPCookie *cookie in all) {
        NSLog(@"Name: %@ : Value: %@", cookie.name, cookie.value);
        NSLog(@"Comment: %@ : CommentURL: %@", cookie.comment, cookie.commentURL);
        NSLog(@"Domain: %@ : ExpiresDate: %@", cookie.domain, cookie.expiresDate);
        NSLog(@"isHTTPOnly: %c : isSecure: %c", cookie.isHTTPOnly, cookie.isSecure);
        NSLog(@"isSessionOnly: %c : path: %@", cookie.isSessionOnly, cookie.path);
        NSLog(@"portList: %@ : properties: %@", cookie.portList, cookie.properties);
        NSLog(@"version: %u", cookie.version);
    }
} 

I also found this code to use these cookies, but I'm not sure how to use it:

我也找到了这个代码来使用这些cookie,但是我不知道如何使用:

[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookies];

Here is my method for POSTing, I am using RestKit API:

这是我发布的方法,我使用的是RestKit API:

- (IBAction)addLinkPressed:(UIButton *)sender {

        [RKClient clientWithBaseURLString:@"http://MyWebsite.com"];

        NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:
                                self.linkField.text, @"url",
                                self.linkTitleField.text, @"title",
                                self.linkSummaryField.text, @"summary",
                                nil];

        RKRequest *request = [[RKClient sharedClient] post:@"/send_link.php" params:params delegate:self];
        [request setUserData:@"sendLink"];   
}

Question: Which property of cookies should I store to use it for login information and where should I put it in my code?

问:我应该存储哪些cookie属性来使用它作为登录信息,我应该将它放在代码中的什么位置?

1 个解决方案

#1


1  

I solved this issue by some inefficient way. Here is my methodology: First I try to post to the web service and after posting I parse the returning HTML to see if the posting was successful or not. If posting was successful I give an appropriate message to the user that you post successfully but if it was not successful it could have two reasons: First: there were some error during the post execution Second: the user was not logged in. The way that I recognize the differentiation between fist and second error is just parsing the response HTML. Here is the code that I used for this methodology (this is for the time that the user wants to change the password)

我用一些低效的方法解决了这个问题。这里是我的方法:首先我尝试发布到web服务,然后在发布后解析返回的HTML,看看发布是否成功。如果发布成功,我将向用户发送一条适当的消息,告诉他们您已经成功发布了,但是如果没有成功发布,可能有两个原因:第一,在发布执行过程中出现了一些错误;第二,用户没有登录。识别第一个和第二个错误之间的区别的方法就是解析响应HTML。下面是我用于此方法的代码(这是用户想要更改密码的时间)

- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error {
    NSRange range = [[error localizedDescription] rangeOfString:@"-1012"];
    if (range.length > 0){
        //First error occurs here
    }
    RKLogError(@"Hit error: %@", error);
}



- (IBAction)requestToChangePasswordPressed {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"Loading";

    [RKClient clientWithBaseURLString:@"http://WebServiceDomain.com"];

    NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:
                            self.oldPasswordField.text, @"oldPassword",
                            self.passwordNew.text, @"newPassword",
                            self.confirmPasswordField.text, @"confirmPassword",
                            nil];

    RKRequest *request = [[RKClient sharedClient] post:@"/change_password.php" params:params delegate:self];
    [request setUserData:@"changePassword"];
    [self.view endEditing:YES];
    [MBProgressHUD hideHUDForView:self.view animated:YES];
}

- (void)autoLogin {

    [RKClient clientWithBaseURLString:@"http://WebServiceDomain.com"];
    [RKObjectManager sharedManager].client=[RKClient sharedClient];
    RKParams *parameters = [RKParams params];

    [parameters setValue:[[NSUserDefaults standardUserDefaults] objectForKey:@"defaultUsername"] forParam:@"username"];
    [parameters setValue:[[NSUserDefaults standardUserDefaults] objectForKey:@"defaultPassword"] forParam:@"password"];

    [[RKClient sharedClient] setAuthenticationType:RKRequestAuthenticationTypeHTTP];
    // because we have two POSTs and we want to use the same method for both of the for didLoadResponse: we set the UserDate like bellow
    RKRequest *request = [[RKClient sharedClient] post:@"/login.php" params:parameters delegate:self];
    [request setUserData:@"login"];
}


- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response
{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"Loading";

    id userData = [request userData];
    if ([userData isEqual:@"login"]) {

        if ([request isGET]) {
            // Handling GET /foo.xml

            if ([response isOK]) {
                // Success! Let's take a look at the data
                NSLog(@"Retrieved XML: %@", [response bodyAsString]);
            }

        } else if ([request isPOST]) {

            // Handling POST /other.json
            if ([response isJSON]) {
                NSLog(@"Got a JSON response back from our POST!");
            }

        } else if ([request isDELETE]) {

            // Handling DELETE /missing_resource.txt
            if ([response isNotFound]) {
                NSLog(@"The resource path '%@' was not found.", [request resourcePath]);
            }
        }
    }

    else if ([userData isEqual:@"sendLink"]) {
        NSData *addLinksHtmlData = response.body;

        // 2
        TFHpple *addlinksParser = [TFHpple hppleWithHTMLData:addLinksHtmlData];

        // 3
        NSString *errorLinksXpathQueryString = @"//div[@class='errorBox']/ul/li";
        NSArray *errorLinksNodes = [addlinksParser searchWithXPathQuery:errorLinksXpathQueryString];


        // 4
        NSMutableArray *newErrorLinks = [[NSMutableArray alloc] initWithCapacity:0];
        for (TFHppleElement *element in errorLinksNodes) {
            // 5
            AllModels *errorTitle = [[AllModels alloc] init];
            [newErrorLinks addObject:errorTitle];

            // 6
            errorTitle.errorTitle = [[element firstChild] content];
        }

        // 8
        self.linkErrorObjects = newErrorLinks;



        NSString *successLinksXpathQueryString = @"//div[@class='successBox']";
        NSArray *successLinksNodes = [addlinksParser searchWithXPathQuery:successLinksXpathQueryString];


        // 4
        NSMutableArray *newSuccessLinks = [[NSMutableArray alloc] initWithCapacity:0];
        for (TFHppleElement *element in successLinksNodes) {
            // 5
            AllModels *successTitle = [[AllModels alloc] init];
            [newSuccessLinks addObject:successTitle];

            // 6
            successTitle.successTitle = [[element firstChild] content];
        }

        // 8
        self.linkSuccessObjects = newSuccessLinks;

    }


    else {

        NSLog(@"HTTP status code:     %d", response.statusCode);
        NSLog(@"HTTP status message:  %@", [response localizedStatusCodeString]);
        NSLog(@"Header fields: %@", response.allHeaderFields);
        NSLog(@"Body: %@", response.bodyAsString);


        NSData *HtmlData = response.body;

        // 2
        TFHpple *addParser = [TFHpple hppleWithHTMLData:HtmlData];

        // 3
        NSString *errorXpathQueryString = @"//div[@class='errorBox']/ul/li";
        NSArray *errorNodes = [addParser searchWithXPathQuery:errorXpathQueryString];


        // 4
        NSMutableArray *newError = [[NSMutableArray alloc] initWithCapacity:0];
        for (TFHppleElement *element in errorNodes) {
            // 5
            AllModels *errorTitle = [[AllModels alloc] init];
            [newError addObject:errorTitle];

            // 6
            errorTitle.errorTitle = [[element firstChild] content];
        }

        // 8
        self.ErrorObjects = newError;



        NSString *successXpathQueryString = @"//div[@class='successBox']";
        NSArray *successNodes = [addParser searchWithXPathQuery:successXpathQueryString];


        // 4
        NSMutableArray *newSuccess = [[NSMutableArray alloc] initWithCapacity:0];
        for (TFHppleElement *element in successNodes) {
            // 5
            AllModels *successTitle = [[AllModels alloc] init];
            [newSuccess addObject:successTitle];

            // 6
            successTitle.successTitle = [[element firstChild] content];
        }

        // 8
        self.successObjects = newSuccess;

        [self errorCheck];
    }
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    [MBProgressHUD hideHUDForView:self.view animated:YES];
}



- (void)errorCheck {

    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"Loading";

    if(self.errorObjects.count > 0) {
        AllModels *errorlink = [self.errorObjects objectAtIndex:0];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"There is a problem" message:errorlink.errorTitle delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil , nil];
        [alert show];
    }
    else {


        if(self.linkErrorObjects.count > 0) {
            [self autoLogin];
            [self requestToChangePasswordPressed];
        }

        else {
            AllModels *successlink = [self.successObjects objectAtIndex:0];
            self.successLabel.hidden = NO;
            self.successLabel.text = successlink.successTitle;
            NSLog(@"Success Title:  %@",successlink.successTitle);


            [UIView animateWithDuration:3.0
                                  delay:0.0
                                options:UIViewAnimationOptionBeginFromCurrentState
                             animations:^{ self.successLabel.alpha = 0.0; }
                             completion:^(BOOL fin) { if (fin) [self.successLabel removeFromSuperview]; }];


            [self performSelector:@selector(dismissModalViewController) withObject:nil afterDelay:1.0];
        }

    }
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    [MBProgressHUD hideHUDForView:self.view animated:YES];
}

#1


1  

I solved this issue by some inefficient way. Here is my methodology: First I try to post to the web service and after posting I parse the returning HTML to see if the posting was successful or not. If posting was successful I give an appropriate message to the user that you post successfully but if it was not successful it could have two reasons: First: there were some error during the post execution Second: the user was not logged in. The way that I recognize the differentiation between fist and second error is just parsing the response HTML. Here is the code that I used for this methodology (this is for the time that the user wants to change the password)

我用一些低效的方法解决了这个问题。这里是我的方法:首先我尝试发布到web服务,然后在发布后解析返回的HTML,看看发布是否成功。如果发布成功,我将向用户发送一条适当的消息,告诉他们您已经成功发布了,但是如果没有成功发布,可能有两个原因:第一,在发布执行过程中出现了一些错误;第二,用户没有登录。识别第一个和第二个错误之间的区别的方法就是解析响应HTML。下面是我用于此方法的代码(这是用户想要更改密码的时间)

- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error {
    NSRange range = [[error localizedDescription] rangeOfString:@"-1012"];
    if (range.length > 0){
        //First error occurs here
    }
    RKLogError(@"Hit error: %@", error);
}



- (IBAction)requestToChangePasswordPressed {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"Loading";

    [RKClient clientWithBaseURLString:@"http://WebServiceDomain.com"];

    NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:
                            self.oldPasswordField.text, @"oldPassword",
                            self.passwordNew.text, @"newPassword",
                            self.confirmPasswordField.text, @"confirmPassword",
                            nil];

    RKRequest *request = [[RKClient sharedClient] post:@"/change_password.php" params:params delegate:self];
    [request setUserData:@"changePassword"];
    [self.view endEditing:YES];
    [MBProgressHUD hideHUDForView:self.view animated:YES];
}

- (void)autoLogin {

    [RKClient clientWithBaseURLString:@"http://WebServiceDomain.com"];
    [RKObjectManager sharedManager].client=[RKClient sharedClient];
    RKParams *parameters = [RKParams params];

    [parameters setValue:[[NSUserDefaults standardUserDefaults] objectForKey:@"defaultUsername"] forParam:@"username"];
    [parameters setValue:[[NSUserDefaults standardUserDefaults] objectForKey:@"defaultPassword"] forParam:@"password"];

    [[RKClient sharedClient] setAuthenticationType:RKRequestAuthenticationTypeHTTP];
    // because we have two POSTs and we want to use the same method for both of the for didLoadResponse: we set the UserDate like bellow
    RKRequest *request = [[RKClient sharedClient] post:@"/login.php" params:parameters delegate:self];
    [request setUserData:@"login"];
}


- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response
{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"Loading";

    id userData = [request userData];
    if ([userData isEqual:@"login"]) {

        if ([request isGET]) {
            // Handling GET /foo.xml

            if ([response isOK]) {
                // Success! Let's take a look at the data
                NSLog(@"Retrieved XML: %@", [response bodyAsString]);
            }

        } else if ([request isPOST]) {

            // Handling POST /other.json
            if ([response isJSON]) {
                NSLog(@"Got a JSON response back from our POST!");
            }

        } else if ([request isDELETE]) {

            // Handling DELETE /missing_resource.txt
            if ([response isNotFound]) {
                NSLog(@"The resource path '%@' was not found.", [request resourcePath]);
            }
        }
    }

    else if ([userData isEqual:@"sendLink"]) {
        NSData *addLinksHtmlData = response.body;

        // 2
        TFHpple *addlinksParser = [TFHpple hppleWithHTMLData:addLinksHtmlData];

        // 3
        NSString *errorLinksXpathQueryString = @"//div[@class='errorBox']/ul/li";
        NSArray *errorLinksNodes = [addlinksParser searchWithXPathQuery:errorLinksXpathQueryString];


        // 4
        NSMutableArray *newErrorLinks = [[NSMutableArray alloc] initWithCapacity:0];
        for (TFHppleElement *element in errorLinksNodes) {
            // 5
            AllModels *errorTitle = [[AllModels alloc] init];
            [newErrorLinks addObject:errorTitle];

            // 6
            errorTitle.errorTitle = [[element firstChild] content];
        }

        // 8
        self.linkErrorObjects = newErrorLinks;



        NSString *successLinksXpathQueryString = @"//div[@class='successBox']";
        NSArray *successLinksNodes = [addlinksParser searchWithXPathQuery:successLinksXpathQueryString];


        // 4
        NSMutableArray *newSuccessLinks = [[NSMutableArray alloc] initWithCapacity:0];
        for (TFHppleElement *element in successLinksNodes) {
            // 5
            AllModels *successTitle = [[AllModels alloc] init];
            [newSuccessLinks addObject:successTitle];

            // 6
            successTitle.successTitle = [[element firstChild] content];
        }

        // 8
        self.linkSuccessObjects = newSuccessLinks;

    }


    else {

        NSLog(@"HTTP status code:     %d", response.statusCode);
        NSLog(@"HTTP status message:  %@", [response localizedStatusCodeString]);
        NSLog(@"Header fields: %@", response.allHeaderFields);
        NSLog(@"Body: %@", response.bodyAsString);


        NSData *HtmlData = response.body;

        // 2
        TFHpple *addParser = [TFHpple hppleWithHTMLData:HtmlData];

        // 3
        NSString *errorXpathQueryString = @"//div[@class='errorBox']/ul/li";
        NSArray *errorNodes = [addParser searchWithXPathQuery:errorXpathQueryString];


        // 4
        NSMutableArray *newError = [[NSMutableArray alloc] initWithCapacity:0];
        for (TFHppleElement *element in errorNodes) {
            // 5
            AllModels *errorTitle = [[AllModels alloc] init];
            [newError addObject:errorTitle];

            // 6
            errorTitle.errorTitle = [[element firstChild] content];
        }

        // 8
        self.ErrorObjects = newError;



        NSString *successXpathQueryString = @"//div[@class='successBox']";
        NSArray *successNodes = [addParser searchWithXPathQuery:successXpathQueryString];


        // 4
        NSMutableArray *newSuccess = [[NSMutableArray alloc] initWithCapacity:0];
        for (TFHppleElement *element in successNodes) {
            // 5
            AllModels *successTitle = [[AllModels alloc] init];
            [newSuccess addObject:successTitle];

            // 6
            successTitle.successTitle = [[element firstChild] content];
        }

        // 8
        self.successObjects = newSuccess;

        [self errorCheck];
    }
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    [MBProgressHUD hideHUDForView:self.view animated:YES];
}



- (void)errorCheck {

    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"Loading";

    if(self.errorObjects.count > 0) {
        AllModels *errorlink = [self.errorObjects objectAtIndex:0];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"There is a problem" message:errorlink.errorTitle delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil , nil];
        [alert show];
    }
    else {


        if(self.linkErrorObjects.count > 0) {
            [self autoLogin];
            [self requestToChangePasswordPressed];
        }

        else {
            AllModels *successlink = [self.successObjects objectAtIndex:0];
            self.successLabel.hidden = NO;
            self.successLabel.text = successlink.successTitle;
            NSLog(@"Success Title:  %@",successlink.successTitle);


            [UIView animateWithDuration:3.0
                                  delay:0.0
                                options:UIViewAnimationOptionBeginFromCurrentState
                             animations:^{ self.successLabel.alpha = 0.0; }
                             completion:^(BOOL fin) { if (fin) [self.successLabel removeFromSuperview]; }];


            [self performSelector:@selector(dismissModalViewController) withObject:nil afterDelay:1.0];
        }

    }
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    [MBProgressHUD hideHUDForView:self.view animated:YES];
}