I'm using NSURLConnection to contact a URL that returns a 302 response along with some data, and then returns a 200 response with some different data. I want to stop it at the 302 response and get the data that comes along with the 302 response.
To make this happen I understand the delegate method [NSURLConnection connection: willSendRequest: redirectResponse:] should return nil. To quote the documentation: "Alternatively, the delegate method can return nil to cancel the redirect, and the connection will continue to process. This has special relevance in the case where redirectResponse is not nil. In this case, any data that is loaded for the connection will be sent to the delegate, and the delegate will receive a connectionDidFinishLoading or connection:didFailLoadingWithError: message, as appropriate." Using tcpdump I can see the 302 response come back, along with the expected data, but I never receive anything from further from NSURLConnection after I return the nil above. No "didReceiveData:" or "connectionDidFinishLoading:" or "connection:didFailLoadingWithError:" - zip, zilch, nada. The code works fine for receiving 200 responses etc. Suggestions please? Slightly edited code below. #pragma mark NSURLConnection delegate methods - (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse { return nil; // NEVER cache responses } - (void)connection:(NSURLConnection *)connection didReceiveData: (NSData *)data { @try { if ( connection && responseData ) [responseData appendData:data]; } @catch ( NSException *e ) { NSLog(@"exception during didReceiveData: %@", e); } } - (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse: (NSURLResponse *)redirectResponse { if(redirectResponse) { NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) redirectResponse; statusCode = [httpResponse statusCode]; if (statusCode==302) newRequest = nil; // go for the first return only } return request; } - (void)connection:(NSURLConnection *)connection didReceiveResponse: (NSURLResponse *)response { @try { NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; statusCode = [httpResponse statusCode]; if (httpResponse) { // we know this is an NSHTTPURLResponse since we sent it to an http:// URL if (pageRequest==1) { // this is the first page request - which should just get the redirect info and go from there statusCode = [httpResponse statusCode]; if (statusCode==302) { if( connection && responseData ) [responseData setLength:0]; } } else if (pageRequest==2) { // this is the second page request statusCode = [httpResponse statusCode]; if (statusCode==200) { // success ... if( connection && responseData ) [responseData setLength:0]; } } } } @catch ( NSException *e ) { NSLog(@"exception during didReceiveResponse: %@", e); } } - (void)connectionDidFinishLoading:(NSURLConnection *)connection{ @try { NSString *tempString = [[NSString alloc] initWithData:responseData encoding:NSMacOSRomanStringEncoding]; [dict setValue:responseData forKey:@"body"]; [dict setValue:tempString forKey:@"bodyString"]; [tempString release]; // [connection release];connection=nil; [responseData release]; } @catch ( NSException *e ) { NSLog(@"exception during pageRequest %d connectionDidFinishLoading: %@",pageRequest, e); } } _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]