Can an ASIHTTPRequest be retried?

有些话、适合烂在心里 提交于 2019-12-10 09:08:06

问题


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



来源:https://stackoverflow.com/questions/6062178/can-an-asihttprequest-be-retried

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!