服务器返回错误503
我在我的应用程序中添加了一项功能,可以将 POST 请求发送到我的网络服务器。我已经测试过它并且运行得很好。但在某个时刻,当我尝试再次运行该应用程序时,它崩溃了。我发现错误 503 表示服务器不可用。我的问题是,当我收到此错误时,我应该采取什么措施来防止我的应用程序崩溃。
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
HTTP 标准对此非常明确:所有以 4 开头的错误代码都是客户端错误,所有以 5 开头的错误代码都是服务器端错误。因此,当您收到 503 时,这是 HTTP 服务器返回给您的客户端的错误,告诉您服务器上有问题,而不是您的客户端或您发送的请求有问题。所以我想知道您的客户端是否真的可以采取任何措施来防止此错误。如果服务器正确遵守 HTTP 标准,则 5xx 错误意味着您必须查看服务器端日志并找出服务器的问题,然后修复此问题(在服务器上)。如果您发送的请求有任何问题,服务器应返回 4xx 错误代码。
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.
如果您使用 NSURLConnection API 执行您的请求(您应该这样做),那么您的委托应该收到
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
打电话。在此方法中,您可以查询 NSURLResponse 的状态代码,并根据状态代码采取相应的操作。
示例:
状态 200 = 好的,继续执行您需要执行的操作
状态 503 = 服务器不可用 - 正常失败,取消请求并自行清理。然后您可以再试一次。
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.