AFNetworking 2.0总是返回403

时间:2015-09-25 15:46:31

标签: ios objective-c json afnetworking-2

我正在使用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并出现此错误:不可接受的内容类型:text / html

奇怪的是,我使用Cocoa GraphicalHttpClient做了同样的请求,它没问题,并返回JSON响应,并且:

Content-Type:application / json; charset = UTF-8 日期:2015年9月25日星期五15:28:27 GMT 转移编码:身份 X-Powered-By:Servlet / 2.5 JSP / 2.1

非常感谢。

2 个答案:

答案 0 :(得分:2)

您的服务器似乎发送了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) {

}];

答案 1 :(得分: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);
    }];