ios - Redirect URL from NSURLConnection -


i trying solve following problem: sending post request additional header values specific url. purpose use nsmutabeurlrequest. works nice when nslog response, need url of redirect. if use request.url in competitionhandler returns url sent post request to, it's not need.

any tips on how url of redirect? (it nice if won't change code significantly.

below have far:

url = [nsurl urlwithstring:@"https://***"]; nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:url]; [request sethttpmethod:@"post"]; [request setvalue:@"application/json; charset=utf-8" forhttpheaderfield:@"content-type"];  //some additional values set here  [request sethttpbody:data];  nsoperationqueue *queue = [[nsoperationqueue alloc] init]; [nsurlconnection  sendasynchronousrequest:request queue:queue completionhandler:^(nsurlresponse *response, nsdata *data, nserror *error) {      nslog(@"%@", response);      if (error)         nslog(@"%s: nsurlconnection error: %@", __function__, error);      nsstring *responsestring = [[nsstring alloc] initwithdata:data encoding:nsutf8stringencoding];     nslog(@"responsestring: %@",responsestring);  }]; 

from apple's developer docs:

a redirect occurs when server responds request indicating client should make new request different url. nsurlsession, nsurlconnection, , nsurldownload classes notify delegates when occurs.

to handle redirect, url loading class delegate must implement 1 of following delegate methods:

for nsurlsession, implement urlsession:task:willperformhttpredirection:newrequest:completionhandler: delegate method.

for nsurlconnection, implement connection:willsendrequest:redirectresponse: delegate method.

for nsurldownload, implement download:willsendrequest:redirectresponse: delegate method. in these methods, delegate can examine new request , response caused redirect, , can return new request object through completion handler nsurlsession or through return value nsurlconnection , nsurldownload.

the delegate can of following:

allow redirect returning provided request. create new request, pointing different url, , return request. reject redirect , receive existing data connection returning nil. in addition, delegate can cancel both redirect , connection. nsurlsession, delegate sending cancel message task object. nsurlconnection or nsurldownload apis, delegate sending cancel message nsurlconnection or nsurldownload object.

the delegate receives connection:willsendrequest:redirectresponse: message if nsurlprotocol subclass handles request has changed nsurlrequest in order standardize format, example, changing request http://www.apple.com http://www.apple.com/. occurs because standardized, or canonical, version of request used cache management. in special case, response passed delegate nil , delegate should return provided request.

so basically, need make use of connection:willsendrequest:redirectresponse: delegate method grab new url , perform whatever operation need on it


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -