NSURLConnection disable "too many HTTP redirects"
I am currently doing many server side computations that take a while to process.
In the mean time I redirect the user to the same page until i开发者_运维问答t has a response.
This is resulting in a "too many HTTP redirects" error. Is there a way to disable this, or increase its threshold?
This happens on the simulator and on the phone.
here is the relevant code:
//Define request
NSURLRequest *request = [requestGenerator theRequest];
// Execute URL and read response
NSError *error = nil;
NSHTTPURLResponse *httpResponse;
NSData *resp = [NSURLConnection sendSynchronousRequest:request returningResponse:&httpResponse error:&error];
//If we get a good response
if(resp != nil && httpResponse != NULL && [httpResponse statusCode] >= 200 && [httpResponse statusCode] < 300)
{
...
}
if (error) {
[self showErrorMessage:error];
}
Redirecting to the same URL is extremely wasteful. At the very least, assign the "job" an ID and return that to the calling app. It can then poll the server, passing up the ID, to see when the job completes. You should not poll too frequently, perhaps once every few seconds.
You can also just use a single request-response. Your server will simply not respond until it has the data. On the iPhone the default time-out is set to 60 seconds. Your server may be able to start a response (write out a little data) so the connection is established; but do not complete & close the connection until processing completes.
Finally, if you do a simply request ... long delay ... response; and expect it will take > 60 seconds - consider extending the timeout of the request via NSMutableURLRequest's setTimeoutInterval.
Note: in your example you are using a synchronous request. If you do that outside of a thread, your app can block. If it blocks for > 20 seconds, it will be killed by the watchdog service. It's very easy to use the asynch methods with delegates to catch the responses, so give them a whirl.
Finally, if you use the asynch methods; you can catch the 3xx redirect and handle it yourself; but you know, your redirect technique seems evil so don't do it.
精彩评论