1

I'm sending a HTTP GET request to an URL using NSURLConnection with the delegate. The request gets redirected with a HTTP 302 and the new request is performed and the data retrieved.

The problem is that I don't want the body of the redirected HTTP request but the content of the original redirect response.

I've implemented - (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response which is called with the redirectRepsonse but connection:didReceiveData is not called until the new redirected request returns.

I've found no solution so far except of using a CFNetwork based approach.

Update: I've created a sample project with the problem for you to play with: https://github.com/snod/302RedirectTest

Any ideas?

snod
  • 2,442
  • 3
  • 20
  • 22

1 Answers1

2

From the docs of -connection:willSendRequest:redirectResponse::

To receive the body of the redirect response itself, return nil to cancel the redirect. The connection continues to process, eventually sending your delegate a connectionDidFinishLoading or connection:didFailLoadingWithError: message, as appropriate.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
  • I already tried that without success. When you return nil after the redirect response has been sent to the delegate the connection will dropped silenty without ever calling the URL connection delegate methods. – snod Sep 05 '14 at 13:57
  • What happens if you return the original request, instead? (Actually, if you get a call for a canonicalized original request, you would probably want to keep that request and return it for a redirect request.) – Ken Thomases Sep 06 '14 at 05:04
  • Does not work either, you get the same redirect response everytime until there have been to many of them and the server bails out. – snod Sep 06 '14 at 10:04