Recuperar respuesta HTTP / códigos de estado HTTP Request en iOS?
-
06-09-2019 - |
Pregunta
Tengo que comprobar y evaluar los códigos de estado HTTP en mi aplicación para el iPhone. Tengo un objeto NSURLRequest y un NSURLConnection que con éxito (creo) conectarse al sitio:
// 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
}
Tengo este código aquí: https://web.archive.org/web/20100908130503/http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks /UsingNSURLConnection.html
Pero no es así (por lo que puedo ver) decir nada sobre cómo acceder a los códigos de estado HTTP.
Como hacer una conexión a un sitio y luego comprobar los códigos de estado HTTP de esa conexión?
Solución
Esto es cómo lo hago:
#pragma mark -
#pragma mark NSURLConnection Delegate Methods
- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response {
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
int responseStatusCode = [httpResponse statusCode];
}
Pero estoy usando un NSURLConnection asíncrona, por lo que no se puede ayudar. Pero espero que lo hace de todos modos!