2
votes

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

2
Hi. Were you able to receive the last redirect URL? Thanks. I am having the same issue.Shobhit Puri

2 Answers

1
votes

Please read Handling Redirects and Other Request Changes in the documentation. Briefly, you'll want to a) provide a delegate for your connection, and b) implement the method connection:willSendRequest:redirectResponse: in said delegate. I don't understand exactly what you're trying to do in your question -- do you just want the data from URL to which your connection is redirected, or do you want to see the redirected request? Either way, implementing the method above should help. Your delegate will be able to see the redirected request, and returning it unchanged should cause the connection to load the new URL.

0
votes

We can achieve this with NSMutableURLRequest. I had followed these steps, it's working fine for me. Go through with below mentioned Link.

URL: Click here: Get the final redirected url

P.S: Let me know if you need further help.