AFNetworking 2.0 always return 403

不羁岁月 提交于 2020-01-06 07:26:11

问题


I'm making a GET request for JSON using AFNetworking 2.0

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

But it always returns 403 with this error: unacceptable content-type: text/html

The weird thing is that the same request I do with Cocoa GraphicalHttpClient, it's ok and returns the JSON response and this:

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

Many thanks.


回答1:


It seems like your server send text/html ContentTypes. To accept this type of content add 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:


This means that your server is sending "text/html" instead of the already supported types. My solution was to add "text/html" to acceptableContentTypes set in AFURLResponseSerialization class. Just search for "acceptableContentTypes" and add @"text/html" to the set manually.

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

Of course, the ideal solution is to change the type sent from the server, but for that you will have to talk with the server team.

in you case you can do like that.

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


来源:https://stackoverflow.com/questions/32785972/afnetworking-2-0-always-return-403

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!