Can someone please post the simplest of working code that can get the last redirected url(nth) when I GET request a url?
I know I need to use asynchronous requests but I am not able to work out a complete working code that solves the problem.
I am using ios5 so I can use the latest added asynchronous inbuilt functionalities in ios5.
I have tried a lot of things but I am not successful still. Using the below code I am trying to send a get request with parameters but somehow those parameters are getting lost due to site's redirection :(!
NSURLRequest *request = [NSURLRequest requestWithURL:urlOriginal];
[NSURLConnection
sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
NSLog("the returned data should be the final data of the redirected url with parameters but it is not");
}];
}
EDIT
So this is what I have done now :
NSURLRequest *requestOriginal = [NSURLRequest requestWithURL:urlOriginal];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:requestOriginal delegate:self];
[connection start];
and delegate :
-(NSURLRequest *)connection:(NSURLConnection *)connection
willSendRequest:(NSURLRequest *)request
redirectResponse:(NSURLResponse *)redirectResponse
{
if (redirectResponse) {
NSMutableURLRequest *r = [request mutableCopy]; // original request
[r setURL: [request URL]];
return r;
} else {
NSLog(@"redirecting to : %@", [request URL]);
return request;
}
}
and the log gives me the correct request url that i need along with the originally passed get parameters.
Now how to I get the data from this request? Another completionHandler delegate method?
edit2 :
Ok so I implemented the other delgates as well :
(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
(void)connectionDidFinishLoading:(NSURLConnection *)connection