views:

221

answers:

2

Hi, I've added a feature in my app that sends POST request to my web server. I've tested it and it does run fine. But at some moment when I tried to run again the app, then it crashes. I've found out the error 503 that says Server is unavailable. My question was what should I do to prevent my app from crashing when I've receive this error.

Thanks

A: 

The HTTP standard is very clear about this: All error codes starting with a 4 are client side errors, all error codes starting with a 5 are server side errors. So when you get 503, this is an error that the HTTP server returns to your client to tell you there is something wrong on/with the server, not with you client or the request you were sending. So I wonder if there is really anything in your client you can do to prevent this error. If there server correctly adheres to the HTTP standard, a 5xx error means you must look at the server side logs and find out what's wrong with the server and then fix this issue (on the server). If there was anything wrong with the request you send, the server should return a 4xx error code.

Mecki
There's nothing the client can do to prevent the error happening, but it can recover gracefully instead of crashing when a 503 is received. See my Answer.
Jasarien
A: 

If you're using the NSURLConnection API to perform your request (which you should be doing), then your delegate should receive the - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response call.

In this method, you can query the NSURLResponse for it's status code, and act accordingly depending what it is.

Example:

Status 200 = OK, go a head with what you need to do Status 503 = Server unavailable - fail gracefully, cancel the request and clean up after yourself. You can then try again.

Jasarien