• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
NSURLConnection willSendRequest: not behaving as expected on a 302 response - no further response after nil return
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSURLConnection willSendRequest: not behaving as expected on a 302 response - no further response after nil return


  • Subject: NSURLConnection willSendRequest: not behaving as expected on a 302 response - no further response after nil return
  • From: Steve Mykytyn <email@hidden>
  • Date: Sat, 4 Oct 2008 19:11:46 -0700

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 (email@hidden)

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:
This email sent to email@hidden


  • Follow-Ups:
    • Re: NSURLConnection willSendRequest: not behaving as expected on a 302 response - no further response after nil return
      • From: Jerry Krinock <email@hidden>
  • Prev by Date: Re: Cocoa Programming for Mac OsX Third Edition eBook search
  • Next by Date: Re: Should not use mogenerator in Mac OS 10.5+ ?
  • Previous by thread: re: Should not use mogenerator in Mac OS 10.5+ ?
  • Next by thread: Re: NSURLConnection willSendRequest: not behaving as expected on a 302 response - no further response after nil return
  • Index(es):
    • Date
    • Thread