AFNetworking 2.0始终返回403 [英] AFNetworking 2.0 always return 403
问题描述
我正在使用AFNetworking 2.0对JSON进行GET请求
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];
但是它总是返回403,并显示以下错误:不可接受的内容类型:text/html
But it always returns 403 with this error: unacceptable content-type: text/html
奇怪的是,我对Cocoa GraphicalHttpClient所做的请求相同,可以并返回JSON响应,如下所示:
The weird thing is that the same request I do with Cocoa GraphicalHttpClient, it's ok and returns the JSON response and this:
内容类型:application/json; charset = UTF-8 日期:2015年9月25日星期五15:28:27 GMT 传输编码:身份 X-Powered-By:Servlet/2.5 JSP/2.1
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
非常感谢.
推荐答案
您的服务器似乎发送了text/html ContentTypes.
要接受这种类型的内容,请添加text/html acceptableContentTypes
.
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) {
}];
这篇关于AFNetworking 2.0始终返回403的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!