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

标签 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 不能?

最佳答案

作为引用,因为通过谷歌搜索结果很高...

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

NSURLErrorBadServerResponse = -1011

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

关于iphone - 我使用 AFNetWorking 时出现错误代码 -1011,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7991927/

相关文章:

iphone - 如何以编程方式访问通知设置

ios - 如何添加从启动屏幕到初始 View Controller ios 的过渡?

iphone - 调试 NSNotificationCenter 问题的方法?

windows - 在哪里可以找到所有 Windows 错误代码的列表?

php - AFNetworking 将 JSON 数组发布为多个单条目字典

ios - Flutter iOS 版本和配置文件构建无法正常工作

php - 异常代码的严重性

WSO2 ESB 未知错误代码 102511

ios - AFHTTPSessionManager GET 卡住/陷入僵局?

ios - 刚刚进入后台的应用程序如何处理延迟的 HTTP 响应?