Showing posts with label NSHTTPURLResponse. Show all posts
Showing posts with label NSHTTPURLResponse. Show all posts

Saturday, May 11, 2013

NSHTTPURLResponse statusCode example ios


statusCode

Returns the receiver’s HTTP status code.
- (NSInteger)statusCode
Return Value
The receiver’s HTTP status code.
Example of [NSHTTPURLResponse statusCode]
- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
   NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
   int code = [httpResponse statusCode];
}
Example of [NSHTTPURLResponse statusCode]
- (void)connection:(NSURLConnection *)aConnection didReceiveResponse:(NSURLResponse*)response 
{
    if ([response isKindOfClass: [NSHTTPURLResponse class]])
        statusCode = [(NSHTTPURLResponse*) response statusCode];
}
Example of [NSHTTPURLResponse statusCode]
if (response) {
    NSHTTPURLResponse* newResp = (NSHTTPURLResponse*)response;
    NSLog(@"%d", newResp.statusCode);
}
else {
    NSLog(@"No response received");
}

NSHTTPURLResponse allHeaderFields example ios


allHeaderFields

Returns all the HTTP header fields of the receiver.
- (NSDictionary *)allHeaderFields
Return Value of [NSHTTPURLResponse allHeaderFields]
A dictionary containing all the HTTP header fields of the receiver. By examining this dictionary clients can see the “raw” header information returned by the HTTP server.
Example of [NSHTTPURLResponse allHeaderFields]
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSString *lastModifiedString = nil;
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  if ([httpResponse respondsToSelector:@selector(allHeaderFields)]) {
    lastModifiedString = [[httpResponse allHeaderFields] objectForKey:@"Last-Modified"];
  }
  // [Here is where the formatting-date-code and downloading would take place]
}
Example of [NSHTTPURLResponse allHeaderFields]
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
  NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  NSString *lastModifiedString = [[httpResponse allHeaderFields] objectForKey:@"Last-Modified"];
  // [Here is where the formatting-date-code and downloading would take place]
}

NSHTTPURLResponse localizedStringForStatusCode example ios


localizedStringForStatusCode:

Returns a localized string corresponding to a specified HTTP status code.
+ (NSString *)localizedStringForStatusCode:(NSInteger)statusCode
Parameters
statusCode
The HTTP status code.
Return Value of [NSHTTPURLResponse localizedStringForStatusCode]
A localized string suitable for displaying to users that describes the specified status code.
Example of [NSHTTPURLResponse localizedStringForStatusCode]
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

    // cast the response to NSHTTPURLResponse so we can look for 404 etc
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;

    if ([httpResponse statusCode] >= 400) {
        // do error handling here
        NSLog(@"remote url returned error %d %@",[httpResponse statusCode],[NSHTTPURLResponse localizedStringForStatusCode:[httpResponse statusCode]]);
    } else {
        // start recieving data
    }
}
Example of [NSHTTPURLResponse localizedStringForStatusCode]
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

    NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)response;
    NSInteger statusCode = [HTTPResponse statusCode];

    if (404 == statusCode || 500 == statusCode) {
        //[self.controller setTitle:@"Error Getting Parking Spot ....."];
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:FALSE];

        [connection cancel];
        NSLog(@"Server Error - %@", [NSHTTPURLResponse localizedStringForStatusCode:statusCode]);
    } else {
        if ([self.requestType isEqualToString:@"GET"])
            [_responseData setLength:0];
    }
}