使用AFNetWorking时出现错误代码-1011

时间:2011-11-03 08:25:09

标签: iphone error-code afnetworking

我在我们的客户公司做服务。我尝试通过AFNetWorking从他们的服务器获取一些信息(我们的客户鼓励使用AFNetWorking) 我使用AFNetWorking做了一些示例,这是有效的。 但是当我使用我们的一个客户URL获取JSON数据时,它失败了,这是错误描述:

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}

我试图找出一些解决方案,但我还无法修复。这是我的代码:

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];

感谢阅读,非常感谢任何帮助或回复。

编辑:正如DarkDust所说:服务器拒绝我的访问权限。但我可以通过基本连接从服务器获取数据: 以下是获取的代码:

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]];
}

我想知道为什么rssConnection可以获取JSON文本而AFHTTPClient不能?

2 个答案:

答案 0 :(得分:23)

作为参考因为谷歌的搜索结果很高......

对于正在寻找通过AFNetworking检索的可能错误代码的其他人,请参阅URL Loading System Error Codes的苹果文档,因为它们是相同的。

NSURLErrorBadServerResponse = -1011

URL加载系统从服务器接收错误数据时返回。 这相当于HTTP服务器发送的“500 Server Error”消息。

答案 1 :(得分:1)

服务器正在响应HTTP错误代码403,这意味着禁止。它拒绝你访问。您需要找出原因,例如通过阅读服务器日志(如果可以)或要求服务器管理员帮助您。可能是需要解除/修改的服务器上的访问限制。

编辑:HTTP POST是一种想要在服务器上保存内容的操作。虽然正常GET似乎根据您编辑的问题正常工作,但目前禁止保存。首先要做的是检查服务器配置。此外,如果您的URL指向一个脚本(JSP,ASP,等等),这是您的唯一有意义的事情,您需要检查它以确定它拒绝您访问的原因(如果服务器配置尚未拒绝)它,它必须是脚本)。