Estoy haciendo un servicio en nuestra compañía de clientes. Y trato de obtener algo de información de su servidor mediante AFNetWorking (Nuestro cliente recomienda usar AFNetWorking) Hice algunas muestras usando AFNetWorking, y es un trabajo. Pero cuando se utiliza una de nuestras direcciones URL de los clientes para obtener datos JSON, fracasó y esto es una descripción del error:Código de error -1011 cuando uso AFNetWorking
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}
trato de encontrar alguna solución, pero no puedo arreglar todavía. Ahí está mi código:
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];
Gracias por leer, y cualquier ayuda o respuesta será muy apreciada.
EDITAR: Como dijo DarkDust: el servidor niega mi acceso. Pero puedo recuperar los datos desde un servidor mediante una conexión básica: Aquí es código para obtener:
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]];
}
Me pregunto por qué rssConnection puede obtener el texto JSON y AFHTTPClient no se puede?
¿De verdad crees que es una buena idea publicar detalles internos/privados de tu cliente sin ofuscación? – DarkDust
Esta es solo una página de prueba, no una página privada. Habrá más cambios antes de que pueda terminar. Y en el futuro, necesitará la clave de API para acceder, y por supuesto, la escondo – magichero
Aún así, todos ahora pueden ver quién es su cliente. Es posible que su cliente no esté muy contento con eso. – DarkDust