Tutorial :GET http request method after redirect when using nsurlconnection SynchronousRequest



Question:

Setting NSMutableURLRequest with setHTTPMethod:@"HEAD", as i only want the HEAD information, and using NSURLConnection with sendSynchronousRequest, as i need the thread to block until the info is retrieved, all works well until URL redirect occurs. It appears that synchronousRequest uses a GET request after it encounters a redirect and downloads the content in the background (Not what i want). Need someone to shine some light on this. Either how to do synchronous request of just the HEAD after redirect or how to block NSOperation (where an NSURLConnection is used) until NSURLConnection asynchronously fetches the header information.

Thanks


Solution:1

You need to instantiate an NSURLConnection and use it with a delegate. I thought by default redirects did the right thing, but if not, implement -connection:willSendRequest:redirectResponse: to ensure the new request is a HEAD.

You can either:

  • Implement this on the main thread
  • Subclass NSOperation to be "concurrent" and run the connection asynchronously there
  • Run the runloop until the connection finishes, creating the equivalent of a synchronous connection

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »