4

Is there a way to retry an ASIHTTPRequest? I'm using custom authentication in a REST service instead of basic HTTP authentication and I would like to retry a request if the session has expired. Calling startAsynchronous a second time on a request causes an exception. I would like to do something like this:

[request setCompletionBlock:^{
        if ([request responseStatusCode] == 500)
        {
            // try to login again in case token expired
            [server loginAndRetryRequest:request];
        } else {
            // parse response
        }
    }];

loginAndRetryRequest: will do another ASIHTTPRequest to login and when it is complete it will start the original request again from it's CompletionBlock (assuming this is possible somehow)?

Rn222
  • 2,167
  • 2
  • 20
  • 36

1 Answers1

5

It should be possible to make a copy of the request and then execute -startAsynchronous again on the copy.

Support for NSCopying protocol was added in release 1.5, which also includes automatic retry in case of timeout (selector -setNumberOfTimesToRetryOnTimeout:.

Another option could be checking their source code to see how the automatic retry is done in case of timeout, but copying and re-issuing the request should work (that was the reason to add support for NSCopying in the first place).

sergio
  • 68,819
  • 11
  • 102
  • 123
  • 2
    Copying the request works, but only if I use setDidFinishSelector instead of setCompletionBlock. If I use a block, it never gets called in the copy. ASIHTTPRequest's copyWithZone doesn't seem to copy the completion block and it doesn't work when I try to do it myself either. Maybe this is a bug? At least I have a way to do it now. Thanks! – Rn222 May 19 '11 at 18:46