如何在objective-c中重定向期间修改请求的cookie?

时间:2011-03-02 19:58:04

标签: objective-c nsurlrequest

我正在进行简单的登录,并注意到在重定向期间,我只有3个必需的cookie中的2个才能正确进入。我捕获了另一个cookie并将它们放在一起但由于某种原因我无法动态修改标题?

- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSHTTPURLResponse *)response {
    NSURL* redirected_url = [request URL];
    NSString* querystr = [redirected_url absoluteString];

    if (response != nil) {
        NSArray* zzzz = [NSHTTPCookie 
                         cookiesWithResponseHeaderFields:[response allHeaderFields] 
                         forURL:[NSURL URLWithString:@""]];

        if ([zzzz count] > 0) {
            if ([querystr isEqualToString:@"https://www.localhost.com/specificurl.aspx"]) {
                NSMutableArray* actualCookies = [[NSMutableArray alloc] init];

                NSHTTPCookie* obj = [self.tmpCookies objectAtIndex:0];
                [actualCookies addObject:obj];
                [actualCookies addObject:zzzz];

                NSArray* authToken = [[NSArray alloc] initWithArray:actualCookies];

                //BLOWS UP HERE ?? NSDictionary* headers = [NSHTTPCookie requestHeaderFieldsWithCookies:authToken];
                //[request setAllHTTPHeaderFields:authToken];

                [viewController setAuthCookieAfterValidLogin:zzzz];
            }
        }
    }

    return request;
}

一般的想法是将此标头设置为具有我的组合Cookie的值

1 个答案:

答案 0 :(得分:3)

我发现虽然我无法修改现有请求,但这并没有阻止我创建新请求并只是返回该请求:)

- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSHTTPURLResponse *)response {
    NSURL* redirected_url = [request URL];
    NSString* querystr = [redirected_url absoluteString];

    if (response != nil) {
        NSArray* zzzz = [NSHTTPCookie 
                         cookiesWithResponseHeaderFields:[response allHeaderFields] 
                         forURL:[NSURL URLWithString:@""]];

        if ([zzzz count] > 0) {
            if ([querystr isEqualToString:@"https://www.localhost.com/specificurl.aspx"]) {
                NSMutableArray* actualCookies = [[NSMutableArray alloc] init];
                NSUInteger i, count = [zzzz count];
                for (i = 0; i < count; i++) {
                    NSHTTPCookie* xxx = [zzzz objectAtIndex:i];
                    [actualCookies addObject:xxx];
                }

                NSHTTPCookie* obj = [self.tmpCookies objectAtIndex:0];
                [actualCookies addObject:obj];

                NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:actualCookies];

                NSURL *url = [NSURL URLWithString:@"https://www.localhost.com/specificurl.aspx"];
                NSMutableURLRequest* xrequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

                [xrequest setHTTPMethod:@"GET"];
                [xrequest setAllHTTPHeaderFields:headers];
                [xrequest setValue:@"https://www.localhost.com/Default.aspx?Site_ID=500000" forHTTPHeaderField: @"Referer"];

                [viewController setAuthCookieAfterValidLogin:zzzz];

                return xrequest;
            }
        }
    }

    return request;
}