1
votes

I am using NSURLConnection to send an HTTP request and running it with [[NSURLConnection alloc] initWithRequest:request delegate:self]; where "request" is a configured NSMutableURLRequest object. Upon an HTTP error based on error code (404 or 500), I want to retry the request.

I get the error response and HTTP status code in the "connection: didReceiveResponse" delegate methods. How should I implement the retry request?

Thanks in advance!

P.S: I tried canceling the connection and starting it on receiving an error but it has no effect as NSURLConnection releases the delegate after it finishes loading or is cancelled.

-----UPDATE-----

    -(void)doHTTPGet:(NSString *)url delegate:(id  <NSURLConnectionDelegate>)delegate timeout:(NSTimeInterval)timeout
{
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]
                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                       timeoutInterval:timeout];
    [request setHTTPMethod:@"GET"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

    // send request by instanciating NSURLConnection
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:delegate];

}

In my model class, I have the caller method and delegates

Caller:

[MyModel doHTTPGet:url delegate:self timeout:HTTP_TIMEOUT];

Delegate Methods:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
    httpResponseStatusCode = [httpResponse statusCode];
    DLOG(@"HTTP status code=%ld", (long)httpResponseStatusCode);

    if (httpResponseStatusCode == 404)
    {
       // RETRY HERE
    } 
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [httpResponseData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{

    // some code PROCESSING RESPONSE
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
        }
3

3 Answers

1
votes

I wrap the NSURLConnection functionality stuff inside another class (in my case, called APIGetter), which has its own delegate protocol and methods and handles things like retrying.

If you don't want to do that, then you'll need to save the parameters and URL, and try again, perhaps in connectionDidFinishLoading:.

Though, I'm trying to imagine a scenario where you'd want to retry on a 4xx HTTP error. Seems more likely on a connection error, so you'd put it in connection:didFailWithError.

0
votes

Inside your response just use a switch or if statement to check for the errors you wish and if your response is 404 or 500 reload the request

This is pseudo code:

if error contains string(404 or 500) {

//call the function which loads the request

}

0
votes

I would recommend STNetTaskQueue for you as it provides retry time and retry interval for each net task.

What's more, it can pack request parameters from a request object automatically, and max concurrent net task is also configurable.