Récupérer réponse HTTP / HTTP Demande des codes d'état dans iOS?
-
06-09-2019 - |
Question
Je dois vérifier et évaluer les codes d'état HTTP dans mon application iPhone. J'ai un objet NSURLRequest et un NSURLConnection qui (je pense) avec succès vous connecter au site:
// 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
}
Je suis ici ce code: https://web.archive.org/web/20100908130503/http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks /UsingNSURLConnection.html
Mais il n'a pas (pour autant que je peux voir) dire quoi que ce soit sur l'accès aux codes d'état HTTP.
Comment établir une connexion à un site, puis vérifier les codes d'état HTTP de cette connexion?
La solution
Voici comment je le fais:
#pragma mark -
#pragma mark NSURLConnection Delegate Methods
- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response {
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
int responseStatusCode = [httpResponse statusCode];
}
Mais j'utilise un NSURLConnection asynchrone, ce ne peut pas vous aider. Mais je l'espère ne quand même!
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow