Can an ASIHTTPRequest be retried?

2019-05-07 01:12发布

问题:

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)?

回答1:

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).