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 wonder why rssConnection can get JSON text and cannot AFHTTPClient?
iphone afnetworking error-code
magichero
source share