Error code -1011 when I use AFNetWorking

magichero picture magichero · Nov 3, 2011 · Viewed 30.8k times · Source

I am doing a service in our customer company. And I try to get some information from their server by AFNetWorking (Our customer encourage to use AFNetWorking) I did some sample using AFNetWorking, and it's work. But when I use one of our customer URLs to get JSON data, it failed and this is error description:

Error Domain=com.alamofire.networking.error Code=-1011 
"Expected status code <NSIndexSet: 0x7e274f0>[number of indexes: 100 (in 1 ranges), 
indexes: (200-299)], got 403" UserInfo=0x7b64040 {NSErrorFailingURLKey=<url_hidden_for_stackoverflow>, 
NSLocalizedDescription=Expected status code <NSIndexSet: 0x7e274f0>[number of indexes: 100 (in 1 ranges), indexes: (200-299)], got 403}

I try to find out some solution, but I can't fix yet. There's my code:

NSURL *url = [NSURL URLWithString:@"http://example.com/"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
//[httpClient setDefaultHeader:@"Accept" value:@"text/json"];
//NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:CONST_KEY_REGISTER_UNIQUE_KEY, CONST_API_KEY_TEXT,nil];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"path/to/page.json" parameters:nil];
[httpClient release];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    NSString *status = [JSON valueForKey:@"status"];
    if ([status isEqualToString:@"OK"]) {
        NSString *uniqueId = [JSON valueForKey:@"uniqueId"];
        [UserSettings setWithKey:CONST_PROGRAM_UNIQUE_KEY value:uniqueId];
    }
    //NSString *message = [json valueForKey:@"message"];
    //NSString *error = [json valueForKey:@"error"];
    [[LoadingView instance] performSelectorOnMainThread:@selector(removeLoadingView)  withObject:nil waitUntilDone:YES];
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
    NSString *errorString = [error description];
    [[LoadingView instance] performSelectorOnMainThread:@selector(removeLoadingView)  withObject:nil waitUntilDone:YES];
}];
NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
[queue addOperation:operation];

Thanks for reading, and any help or reply will be greatly appreciated.

EDIT: As DarkDust said: server deny my access. But I can get data from server by basic connection: Here is code to get:

NSURL *url = [NSURL URLWithString:@"http://example.com/path/to/page.json"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:CONST_CONNECTION_TIMEOUT];
rssConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
[self performSelectorOnMainThread:@selector(downloadStarted) withObject:nil waitUntilDone:NO];
if (rssConnection != nil) {
    do {
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    } while (!done);
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    // I can get text here, but it is not JSON format
NSString *content = [NSString stringWithUTF8String:[data bytes]];
}

I wonder why rssConnection can get JSON text and AFHTTPClient can not ?

Answer

NSMutableString picture NSMutableString · Oct 18, 2013

As reference because of high search result via google...

For others that are looking for the possible error codes retrieved via AFNetworking, consult the apple documentation for URL Loading System Error Codes as these are the same.

NSURLErrorBadServerResponse = -1011

Returned when the URL Loading system receives bad data from the server. This is equivalent to the “500 Server Error” message sent by HTTP servers.