---
title: "Is there a better way to handle HTTP error statuses?"
author: "handy.wang"
date: "2010-05-20T19:04:36Z"
tags: []
categories: ["移动开发"]
slug: "is-there-a-better-way-to-handle-http-error-statuses"
draft: false
lastmod: "2010-05-20T19:05:42Z"
id: 671107
url: "https://handy-wang.iteye.com/blog/671107"
---

```
if ([response respondsToSelector:@selector(statusCode)])
{
    int statusCode = [((NSHTTPURLResponse *)response) statusCode];
    if (statusCode >= 400)
    {
        [connection cancel];  // stop connecting; no more delegate messages
        NSDictionary *errorInfo
          = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:
            NSLocalizedString(@"Server returned status code %d",@""),
            statusCode]
                                        forKey:NSLocalizedDescriptionKey];
        NSError *statusError
          = [NSError errorWithDomain:NSHTTPPropertyStatusCodeKey
                                code:statusCode
                            userInfo:errorInfo];
        [self connection:connection didFailWithError:statusError];
    }
}
 



```