如何从objective-c中返回302而不是200的Http Post中捕获cookie

如何从objective-c中返回302而不是200的Http Post中捕获cookie,objective-c,http,Objective C,Http,目前,如果我执行HTTPGET或POST,并且它返回200W/a类型的应用程序/json或xml,我可以使用此方法捕获发送回的cookie - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { NSArray* returnedCookies = [NSHTTPCookie cookiesWithResponseHea

目前,如果我执行HTTPGET或POST,并且它返回200W/a类型的应用程序/json或xml,我可以使用此方法捕获发送回的cookie

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
  NSArray* returnedCookies = [NSHTTPCookie 
                  cookiesWithResponseHeaderFields:[response allHeaderFields] 
                  forURL:[NSURL URLWithString:@""]];

    [responseData setLength:0];
}
但是,当请求返回302(重定向)时,如何捕获在init请求期间发送到客户端的cookie?当前,我仅在发生此重定向后,从get请求返回200之后,才收到发送回的cookie

更新

我发现的最终解决方案如下。我需要if语句来验证resp是否有效,因为这个方法在我的实例中总共被调用了3次。一次是init请求,然后是302(我关心的那个),最后是重定向url的最后一个GET请求

- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSHTTPURLResponse *)response {
  if (response != nil) {
    NSArray* cookies = [NSHTTPCookie 
                     cookiesWithResponseHeaderFields:[response allHeaderFields] 
                     forURL:[NSURL URLWithString:@""]];

    if ([zzzz count] > 0) {
      //do something with cookies
    }
  }

  return request;
}

NSURLRequest上有一个委托方法可用于:

当您得到重定向响应时,您可以从中提取cookies