[英]Which method of NSURLConnectionDataDelegate will be summoned in case of HTTP 500 Internal server error?
在 HTTP 500 内部服务器错误的情况下,将调用 NSURLConnectionDataDelegate 的哪种方法?
一般错误可以通过 didFailWithError 方法捕获:
-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
//Your code to handle connection errors
}
但对于
404“未找到”
或者
500内部服务器错误”
应该能够在didReceiveResponse
方法中捕获:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
if ([response respondsToSelector:@selector(statusCode)])
{
int statusCode = [((NSHTTPURLResponse *)response) statusCode];
if (statusCode == 500)
{
[connection cancel]; // stop connecting; no more delegate messages
NSLog(@"didReceiveResponse statusCode with %i", statusCode);
}
}
}
HTTP 500 内部服务器错误
通用错误消息,在遇到意外情况并且没有更适合的特定消息时给出。 此https://en.wikipedia.org/wiki/List_of_HTTP_status_codes和https://www.w3.org/Protocols/HTTP/HTRESP.html 的来源
所以需要在服务器端检查。 这不是你的问题。
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
将在 HTTP 500 内部服务器错误的情况下调用。
您可以检查响应状态并处理内部:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.