Error code -1011 when I use AFNetWorking

I work in our client company. And I'm trying to get some information from their server using AFNetWorking (our client recommends using AFNetWorking) I made some examples using AFNetWorking and it works. But when I use one of our client URLs to get JSON data, this failed and this 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 am trying to find some solution, but so far I can’t fix it. There is 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 answer would be greatly appreciated.

EDIT: As DarkDust said: the server has denied my access. But I can get data from the server over the basic connection: Here is the code to receive:

 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 cannot AFHTTPClient?

+8
iphone afnetworking error-code
source share
2 answers

The server responds with error code 403, which means Forbidden. It does not give you access. You need to find out why, for example, by reading the server logs (if you can) or ask the server administrator to help you. These may be server access restrictions that need to be removed / changed.

Change HTTP POST is an operation that wants to save something on the server. While a normal GET seems to work just fine according to your edited question, saving is forbidden now. The first thing to do is check the server configuration. In addition, if your URL points to a script (JSP, ASP, whatever) that the only thing that makes sense in your case, you need to examine this to determine why it denies you access (if the server configuration does not deny it already, it should be a script).

+1
source share

As a link due to the high search result via google ...

For others who are looking for possible error codes obtained through AFNetworking, refer to the Apple documentation for URL system error URLs , as this is the same.

NSURLErrorBadServerResponse = -1011

Return when the URL loading system receives bad data from the server. This is equivalent to the "500 Server Error" message sent by the HTTP servers.

+23
source share

All Articles