2011-03-02 12 views
1

간단한 로그인을하고 있는데 리디렉션 중 3 개의 필수 쿠키 중 2 개만 제대로 들어갈 수 있음을 알았습니다. 다른 쿠키를 캡처하여 함께 모으기는했지만 어떤 이유로 나는 헤더를 즉시 수정할 수 없습니다.objective-c에서 리디렉션 중 요청의 쿠키를 수정하는 방법은 무엇입니까?

- (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; 
} 

일반적인 생각은 내 결합 쿠키의 값이이 헤더를 설정하는 것입니다

답변

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; 
} 
관련 문제