Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve HTTPResponse/HTTPRequest status codes in iOS?

I need to check and evaluate the HTTP Status Codes in my iPhone app. I've got an NSURLRequest object and an NSURLConnection that successfully (I think) connect to the 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 } 

I got this code here: https://web.archive.org/web/20100908130503/http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html

But it doesn't (as far as I can see) say anything about accessing HTTP Status codes.

How to make a connection to a site and then check the HTTP Status Codes of that connection?

like image 528
cmcculloh Avatar asked May 27 '09 20:05

cmcculloh


People also ask

How do I get swift status code?

How To check response. statusCode in SendSynchronousRequest in Swift The Code is Below : let urlPath: String = "URL_IS_HERE" var url: NSURL = NSURL(string: urlPath) var request: NSURLRequest = NSURLRequest(URL: url) var response: AutoreleasingUnsafeMutablePointer<NSURLResponse?> = nil var error: NSErrorPointer?

What is HTTP status code?

An HTTP status code is a server response to a browser's request. When you visit a website, your browser sends a request to the site's server, and the server then responds to the browser's request with a three-digit code: the HTTP status code.


1 Answers

This is how I do it:

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

But I'm using an asynchronous NSURLConnection, so this may not help you. But I hope it does anyway!

like image 185
Rob Avatar answered Sep 24 '22 13:09

Rob