Подтвердить что ты не робот

Получить коды статуса HTTPResponse/HTTPRequest в iOS?

Мне нужно проверить и оценить коды состояния HTTP в моем приложении для iPhone. У меня есть объект NSURLRequest и NSURLConnection, которые успешно (я думаю) подключаются к сайту:

// create the request
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"]
                        cachePolicy:NSURLRequestUseProtocolCachePolicy
                    timeoutInterval:60.0];
// create the connection with the request
// and start loading the data
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
    // Create the NSMutableData that will hold
    // the received data
    // receivedData is declared as a method instance elsewhere
    receivedData=[[NSMutableData data] retain];
} else {
    // inform the user that the download could not be made
}

Я получил этот код здесь: https://web.archive.org/web/20100908130503/http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html

Но это (насколько я вижу) ничего не говорит о доступе к кодам статуса HTTP.

Как установить соединение с сайтом, а затем проверить коды состояния HTTP этого соединения?

4b9b3361

Ответ 1

Вот как я это делаю:

    #pragma mark -
    #pragma mark NSURLConnection Delegate Methods
    - (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response {
         NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
         int responseStatusCode = [httpResponse statusCode];
    }

Но я использую асинхронное NSURLConnection, так что это может вам не помочь. Но я надеюсь, что это все равно!