objective c - Unable to receive the URL for the last redirect for a GET request ios -



objective c - Unable to receive the URL for the last redirect for a GET request ios -

what want do:

when create get request url, response 302 http response, means redirect. know there multiple redirects(3 4) after initial get request. want lastly url after multiple redirects happen. able receive first redirect url.

what i've tried, answers on:

afnetworking -- redirect url nsurlrequest: how handle redirected post? get lastly redirected url in ios 5? iphone nsurl lastly redirected url

my code sniplet taken answers:

afhttprequestoperationmanager *manager = [afhttprequestoperationmanager manager]; nsstring *urlstring = @"url"; nsdictionary *parameters = @{@"k1": @"v1", @"k2": @"v2"}; nsmutableurlrequest *request_orig = [manager.requestserializer requestwithmethod:@"get" urlstring:[[nsurl urlwithstring:urlstring relativetourl:manager.baseurl] absolutestring] parameters:parameters error:nil]; afhttprequestoperation *operation = [[afhttprequestoperation alloc] initwithrequest:request_orig]; [operation setcompletionblockwithsuccess:^(afhttprequestoperation *operation, id responseobject) { //this first redirect url receive nslog(@"new redirect url: %@",[[[operation response] url] absolutestring]); } failure:^(afhttprequestoperation *operation, nserror *error) { nslog(@"failure: err: %@", error); }]; [operation setredirectresponseblock:^nsurlrequest *(nsurlconnection *connection, nsurlrequest *request, nsurlresponse *redirectresponse) { if (redirectresponse) { nsmutableurlrequest *r = [request_orig mutablecopy]; // original request [r seturl: [request url]]; homecoming r; } else { nslog(@"redirecting : %@", [request url]); homecoming request; } }]; [manager.operationqueue addoperation:operation];

i able receive first redirected url there no 2nd or 3rd redirect after that. should in order allow redirects go on , receive url lastly redirect made. novice in ios dev. i'll appreciate help. thanks.

if want follow redirects, sufficient maintain returning provided request , abort when info starting come through connection.

i wrote time ago osx, should work on ios (w/ arc) without changes:

- (nsurl *)resolvedurlredirectionsforurl:(nsurl *)aurl { nslog(@"started w/ initial url: '%@'", aurl); nsurl *originalurl = aurl; nsurl __block *resolvedurl = nil; if (aurl) { dispatch_semaphore_t __communicationlock = dispatch_semaphore_create(0); nsurlrequest *resolverequest = [nsurlrequest requestwithurl:originalurl cachepolicy:nsurlrequestreloadignoringlocalandremotecachedata timeoutinterval:5. ]; afhttprequestoperation *request = [[afhttprequestoperation alloc] initwithrequest:resolverequest]; [request setredirectresponseblock:^nsurlrequest *(nsurlconnection *connection, nsurlrequest *request, nsurlresponse *redirectresponse) { nslog(@"..request encountered redirection to: '%@'",request.url); if (redirectresponse) resolvedurl = [request.url copy]; homecoming request; }]; afhttprequestoperation __weak *weakrequest = request; [request setdownloadprogressblock:^(nsuinteger bytesread, long long totalbytesread, long long totalbytesexpectedtoread) { [weakrequest cancel]; }]; [request setcompletionblockwithsuccess:^(afhttprequestoperation *operation, id responseobject) { nslog(@"..request successful! (result: %@)",responseobject); dispatch_semaphore_signal(__communicationlock); } failure:^(afhttprequestoperation *operation, nserror *error) { if (operation.response.statuscode == 200) nslog(@"..request successful! (aborted)"); else nslog(@"..request failed! (error: %@)",error); dispatch_semaphore_signal(__communicationlock); }]; [request start]; dispatch_semaphore_wait(__communicationlock, dispatch_time_forever); } nslog(@"resolved url '%@' '%@'",originalurl,resolvedurl); homecoming resolvedurl; }

note: because of semaphore it's not thought run on main thread unaltered it'll block main runloop (and nsurlconnections hate that). hence improve dispatch method phone call global queue.

ios objective-c web-services redirect

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -