【问题标题】:AFNetworking 2.0 always return 403AFNetworking 2.0 总是返回 403
【发布时间】:2015-12-23 12:14:25
【问题描述】:

我正在使用 AFNetworking 2.0 对 JSON 发出 GET 请求

    self.managerMasterFetchDeletes = [AFHTTPRequestOperationManager manager];
        self.managerMasterFetchDeletes.requestSerializer.timeoutInterval = 4.0f;
        self.managerMasterFetchDeletes.requestSerializer = [AFJSONRequestSerializer serializer];
        self.managerMasterFetchDeletes.responseSerializer = [AFJSONResponseSerializer serializer];

NSURL *URL = [NSURL URLWithString:rest];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    op.responseSerializer = [AFJSONResponseSerializer serializer];

    [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (operation.response.statusCode==200){

            NSDictionary *dictionary   = (NSDictionary*)responseObject;
            NSMutableDictionary *responseData = [NSMutableDictionary new];
            if ([dictionary objectForKey:@"responseData"]) {
                responseData = [[dictionary objectForKey:@"responseData"] mutableCopy];
            }
            [responseData setValue:entity forKey:@"entityName"];
            [responseData setValue:[NSNumber numberWithInt:timeStamp] forKey:@"lastSync"];
            [[NSNotificationCenter defaultCenter]
             postNotificationName:notification
             object:nil userInfo:responseData];

        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];

 NSArray *operations = [AFURLConnectionOperation batchOfRequestOperations:[arrayOperations copy] progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {
        NSLog(@"%lu of %lu fetch delete complete", numberOfFinishedOperations, totalNumberOfOperations);
    } completionBlock:^(NSArray *operations) {
        NSLog(@"All operations in batch complete");
        [[NSNotificationCenter defaultCenter]
         postNotificationName:@"MasterFecthDeleteEnd"
         object:nil
         userInfo:nil];
    }];

    [self.managerMasterFetchDeletes.operationQueue addOperations:operations waitUntilFinished:NO];

但它总是返回 403 并出现此错误:不可接受的内容类型:文本/html

奇怪的是,我用 Cocoa GraphicalHttpClient 做的同样的请求,没问题并返回 JSON 响应和这个:

内容类型:application/json;charset=UTF-8 日期:格林威治标准时间 2015 年 9 月 25 日星期五 15:28:27 传输编码:身份 X-Powered-By:Servlet/2.5 JSP/2.1

非常感谢。

【问题讨论】:

  • 第三方服务器向您发送禁止的错误,而不是AFNetworking 框架...您可能需要向系统管理员请求您想要访问的内容的权限。
  • 添加 manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];。希望它有效,如果它不起作用,您可以发布您的代码吗??
  • 我试过了,但没有成功。

标签: ios objective-c json afnetworking-2


【解决方案1】:

您的服务器似乎发送了 text/html ContentTypes。 要接受此类内容,请添加 text/html acceptableContentTypes

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
[manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

}];

【讨论】:

    【解决方案2】:

    这意味着您的服务器正在发送"text/html" 而不是已经支持的类型。我的解决方案是将"text/html" 添加到AFURLResponseSerialization 类中设置的acceptableContentTypes。只需搜索“acceptableContentTypes”并手动将@"text/html" 添加到集合中。

    op.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    

    当然,理想的解决方案是更改从服务器发送的类型,但为此您必须与服务器团队交谈。

    在你的情况下,你可以这样做。

    NSURL *URL = [NSURL URLWithString:rest];
        NSURLRequest *request = [NSURLRequest requestWithURL:URL];
        AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
        op.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    
        [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
            if (operation.response.statusCode==200){
    
                NSDictionary *dictionary   = (NSDictionary*)responseObject;
                NSMutableDictionary *responseData = [NSMutableDictionary new];
                if ([dictionary objectForKey:@"responseData"]) {
                    responseData = [[dictionary objectForKey:@"responseData"] mutableCopy];
                }
                [responseData setValue:entity forKey:@"entityName"];
                [responseData setValue:[NSNumber numberWithInt:timeStamp] forKey:@"lastSync"];
                [[NSNotificationCenter defaultCenter]
                 postNotificationName:notification
                 object:nil userInfo:responseData];
    
            }
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"Error: %@", error);
        }];
    

    【讨论】:

    • 我试过了。请注意,使用 httpclient 它可以正常工作并返回:
    • Content-Type: application/json;charset=UTF-8 Date: Fri, 25 Sep 2015 15:28:27 GMT Transfer-Encoding: Identity X-Powered-By: Servlet/2.5 JSP/ 2.1
    猜你喜欢
    • 1970-01-01
    • 2017-09-28
    • 2018-08-15
    • 1970-01-01
    • 2021-03-23
    • 2015-10-23
    • 2018-08-28
    • 1970-01-01
    • 2018-08-05
    相关资源
    最近更新 更多