views:

137

answers:

3

opening http://ystatuschecker.com/n/iwall.php?tedad=1 by browser in safari in mac will redirect to http://ystatuschecker.com/n/iwall.php?tedad=5000

i don't know why in my code it's never redirect .

http://yahoo.ir will redirect to http://world.yahoo.com

what's wrong with my URL?

    NSURL *originalUrl=[NSURL URLWithString:@"http://ystatuschecker.com/n/iwall.php?tedad=1"];
NSData *data=nil;  
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:originalUrl cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];
NSURLResponse *response;
NSError *error;
data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSURL *LastURL=[response URL];
[request release];
[error release];

NSLog(@"%@",LastURL);
+1  A: 

I think you should implement the connection:willSendRequest:redirectResponse: delegate method.

From NSURLConnection

Zero or more connection:willSendRequest:redirectResponse: messages will be sent to the delegate before any further messages are sent if it is determined that the download must redirect to a new location. The delegate can allow the redirect, modify the destination or deny the redirect.

Edit

Actually after looking into it the URL you mention doesn't seem to be using a normal http code for a redirect. I'm not sure how they are doing the redirect, but I think that is the issue.

sammcd
i don't know how to use connection:willSendRequest:redirectResponse:plz give me a sample code...
Naeim
A: 

problem solved . i used header("location:htpp://example.com") in php file.

Naeim
Ahh, I didn't realize you had control over the php as well. That would force the proper redirect http status, to fix the problem.
sammcd
A: 

can anyone give me another sample code that can get redirected url which don't use sendSynchronousRequest .

Naeim