【问题标题】:Error code -1011 when I use AFNetWorking使用 AFNetWorking 时出现错误代码 -1011
【发布时间】:2011-12-20 23:20:36
【问题描述】:

我在我们的客户公司提供服务。我尝试通过 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 不能?

【问题讨论】:

  • 您真的认为在不混淆的情况下发布客户的内部/私人详细信息是个好主意吗?
  • 这只是一个测试页面,不是私人页面。在我完成之前会有更多的变化。未来,需要API密钥才能访问,当然,我把它隐藏了
  • 不过,现在每个人都可以看到您的客户是谁。您的客户可能对此不太满意。

标签: iphone error-code afnetworking


【解决方案1】:

作为参考,因为通过谷歌搜索结果很高...

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

NSURLErrorBadServerResponse = -1011

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 您发布的错误消息清楚地显示Expected status code ..., got 403。服务器拒绝您访问。
    • 我编辑我的帖子,因为如果我添加评论,我无法清楚地告诉你我需要做什么。
    • 感谢您的帮助。我的问题已经结束。我们的客户使用此服务器进行测试并给我错误的配置。我可以通过“GET”方法连接测试服务器。测试页面只是文本/纯文本,根本不是 JSON 格式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-18
    • 2014-04-18
    • 1970-01-01
    • 1970-01-01
    • 2017-08-03
    • 2014-05-12
    • 2013-09-28
    相关资源
    最近更新 更多