请求失败:不可接受的内容类型:文本/html使用AFNetworking 2.0

我正在尝试AFNetworking的新版本2.0,我得到上面的错误。知道为什么会这样吗?这是我的代码:

    NSURL *URL = [NSURL URLWithString:kJSONlink];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
op.responseSerializer = [AFJSONResponseSerializer serializer];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
[[NSOperationQueue mainQueue] addOperation:op];

我使用的是Xcode 5.0。

另外,下面是错误消息:

Error: Error Domain=AFNetworkingErrorDomain Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo=0xda2e670 {NSErrorFailingURLKey=kJSONlink, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0xda35180> { URL: kJSONlink } { status code: 200, headers {
Connection = "Keep-Alive";
"Content-Encoding" = gzip;
"Content-Length" = 2898;
"Content-Type" = "text/html";
Date = "Tue, 01 Oct 2013 10:59:45 GMT";
"Keep-Alive" = "timeout=5, max=100";
Server = Apache;
Vary = "Accept-Encoding";
} }, NSLocalizedDescription=Request failed: unacceptable content-type: text/html}

我只是使用kJSONlink隐藏了JSON。这应该返回一个JSON。

177709 次浏览
这意味着你的服务器正在发送"text/html"而不是已经支持的类型。 我的解决方案是在AFURLResponseSerialization类的acceptableContentTypes集合中添加"text/html"。只要搜索"acceptableContentTypes"并手动将@"text/html"添加到集合中

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

将我的RequestOperationManager响应序列化器设置为HTTPResponseSerializer修复了这个问题。

objective - c

manager.responseSerializer = [AFHTTPResponseSerializer serializer];

斯威夫特

manager.responseSerializer = AFHTTPResponseSerializer()

做这个改变意味着我不需要在每个请求中都添加acceptableContentTypes

在服务器端,我添加了:

header('Content-type: application/json');

到我的。php代码,这也解决了这个问题。

我把@jaytrixz的答案/评论更进一步,并将“text/html”添加到现有的类型集。这样,当他们在服务器端将其修复为“application/json”或“text/json”时,我声称它将无缝工作。

  manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];

一个简单的方法来启用接收“文本/纯”内容类型:

manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];

类似地,如果你希望启用"text/html"内容类型:

manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
 UIImage *image = [UIImage imageNamed:@"decline_clicked.png"];
NSData *imageData = UIImageJPEGRepresentation(image,1);




NSString *queryStringss = [NSString stringWithFormat:@"http://119.9.77.121/lets_chat/index.php/webservices/uploadfile/"];
queryStringss = [queryStringss stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];


[MBProgressHUD showHUDAddedTo:self.view animated:YES];




[manager POST:queryStringss parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
{




[formData appendPartWithFileData:imageData name:@"fileName" fileName:@"decline_clicked.png" mimeType:@"image/jpeg"];






}
success:^(AFHTTPRequestOperation *operation, id responseObject)
{






NSDictionary *dict = [responseObject objectForKey:@"Result"];


NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
[MBProgressHUD hideAllHUDsForView:self.view animated:YES];




}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
[MBProgressHUD hideAllHUDsForView:self.view animated:YES];
NSLog(@"Error: %@ ***** %@", operation.responseString, error);
}];

这是我发现的唯一有效的方法

-(void) testHTTPS {
AFSecurityPolicy *securityPolicy = [[AFSecurityPolicy alloc] init];
[securityPolicy setAllowInvalidCertificates:YES];


AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager setSecurityPolicy:securityPolicy];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];


[manager GET:[NSString stringWithFormat:@"%@", HOST] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"%@", string);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}

在我的情况下,我没有控制服务器设置,但我知道它期待“应用程序/json”为“内容类型”。我在iOS客户端是这样做的:

manager.requestSerializer = [AFJSONRequestSerializer serializer];
< p >引用 AFNetworking版本2内容类型错误 < / p >

如果有人正在使用AFHTTPSessionManager,那么可以这样做来解决问题,

我子类化了AFHTTPSessionManager,我这样做,

NSMutableSet *contentTypes = [[NSMutableSet alloc] initWithSet:self.responseSerializer.acceptableContentTypes];
[contentTypes addObject:@"text/html"];
self.responseSerializer.acceptableContentTypes = contentTypes;

我从另一个角度来解决这个问题。

我认为如果服务器发送带有Content-Type: text/html头的JSON数据。这并不意味着服务器人员打算向你发送一些html,但不小心将其更改为JSON。这意味着服务器并不关心Content-Type头文件是什么。所以如果服务器端不关心客户端,你最好忽略Content-Type头文件。忽略AFNetworking中的Content-Type头检查

manager.responseSerializer.acceptableContentTypes = nil;

这样,AFJSONResponseSerializer(默认值)将序列化JSON数据,而不检查响应头中的Content-Type

只要加上这一行:

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

我按照@Andrie的回答在线下试了试,但没用,

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

所以在寻找了更多之后,我做了一些工作来让它成功工作。

这是我的代码剪辑。

AFHTTPRequestOperationManager *operation = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:url];
operation.responseSerializer = [AFJSONResponseSerializer serializer];


AFJSONResponseSerializer *jsonResponseSerializer = [AFJSONResponseSerializer serializer];


NSMutableSet *jsonAcceptableContentTypes = [NSMutableSet setWithSet:jsonResponseSerializer.acceptableContentTypes];
[jsonAcceptableContentTypes addObject:@"text/plain"];
jsonResponseSerializer.acceptableContentTypes = jsonAcceptableContentTypes;
operation.responseSerializer = jsonResponseSerializer;

希望这能帮助到一些人。

我从Swift代码库中使用AFNetworking工作时遇到了类似的问题,所以我只是把这个留在远程情况下,有人像我一样不幸,必须在这样的设置中工作。如果你是,我觉得你,伙计,坚强点!

由于“不可接受的内容类型”,操作失败,尽管我实际上设置了包含问题内容类型值的Set acceptableContentTypes

我的解决方案是调整Swift代码,使其更加Objective-C友好,我猜:

serializer.acceptableContentTypes = NSSet(array: ["application/xml", "text/xml", "text/plain"]) as Set<NSObject>

一个好的问题总是有多个答案,为了减少和帮助你选择正确的答案,在这里我也加入了我自己的答案。我已经测试过了,工作正常。

AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://www.yourdomain.com/appname/data/ws/index.php/user/login/"]];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];


[manager POST:@"POST" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *json = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"%@", json);
//Now convert json string to dictionary.
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@", error.localizedDescription);
}];