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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19114623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:55:31  来源:igfitidea点击:

Request failed: unacceptable content-type: text/html using AFNetworking 2.0

iosafnetworkingafnetworking-2

提问by jaytrixz

I'm trying out the new version 2.0 of AFNetworking and I'm getting the error above. Any idea why this is happening? Here's my code:

我正在试用 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];

I'm using Xcode 5.0.

我正在使用 Xcode 5.0。

Also, here's the error message:

此外,这是错误消息:

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}

I just hid the JSON using kJSONlink. This should return a JSON.

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

回答by Andrei Neag

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

这意味着您的服务器正在发送"text/html"而不是已经支持的类型。我的解决办法是添加"text/html"acceptableContentTypesAFURLResponseSerialization类。只需搜索“acceptableContentTypes”并@"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.

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

回答by Danpe

Setting my RequestOperationManagerResponse Serializer to HTTPResponseSerializerfixed the issue.

设置我的RequestOperationManager响应序列化程序来HTTPResponseSerializer解决这个问题。

Objective-C

目标-C

manager.responseSerializer = [AFHTTPResponseSerializer serializer];

Swift

迅速

manager.responseSerializer = AFHTTPResponseSerializer()

Making this change means I don't need to add acceptableContentTypesto every request I make.

进行此更改意味着我不需要添加acceptableContentTypes到我提出的每个请求中。

回答by mharper

I took @jaytrixz's answer/comment one step further and added "text/html" to the existing set of types. That way when they fix it on the server side to "application/json" or "text/json" I claim it'll work seamlessly.

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

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

回答by Chris Prince

On the server side, I added:

在服务器端,我补充说:

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

into my .php code and this also fixed the problem.

进入我的 .php 代码,这也解决了这个问题。

回答by dopcn

I solve this problem from a different perspective.

我从不同的角度解决这个问题。

I think if the server sends JSON data with Content-Type: text/htmlheader. It doesn't mean the server guy intended to send you some html but accidentally changed to JSON. It does mean the server guy just doesn't care about what the Content-Typeheader is. So if the server guy doesn't care as the client side you better ignore the Content-Typeheader as well. To ignore the Content-Typeheader check in AFNetworking

我想如果服务器发送带有Content-Type: text/html标头的JSON 数据。这并不意味着服务器人员打算向您发送一些 html 但不小心更改为 JSON。这确实意味着服务器人员不关心Content-Type标题是什么。因此,如果服务器人员不关心作为客户端,您最好也忽略Content-Type标头。忽略Content-Type标头签入AFNetworking

manager.responseSerializer.acceptableContentTypes = nil;

In this way the AFJSONResponseSerializer(the default one) will serialize the JSON data without checking Content-Typein response header.

通过这种方式AFJSONResponseSerializer(默认)将序列化 JSON 数据而不检查Content-Type响应头。

回答by da Rocha Pires

A simple way to enable to receive "text/plain" content type:

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

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

Similarly if you wish to enable "text/html" content type:

同样,如果您希望启用“text/html”内容类型:

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

回答by ABS

I tried below line as per @Andrie answer but didn't work,

我按照@Andrie 的回答在下面尝试过,但没有用,

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

so after hunting more, I did work around to get it work successfully.

因此,在狩猎更多之后,我确实设法使其成功运行。

Here is my code snip.

这是我的代码片段。

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;

Hope this will help someone out there.

希望这会帮助那里的人。

回答by Hemang

If someone is using AFHTTPSessionManagerthen one can do like this to solve the issue,

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

I subclassed AFHTTPSessionManagerwhere I'm doing like this,

我子类化AFHTTPSessionManager了我这样做的地方,

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

回答by chrisallick

This is the only thing that I found to work

这是我发现唯一可行的方法

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

回答by uudaddy

In my case, I don't have control over server setting, but I know it's expecting "application/json" for "Content-Type". I did this on the iOS client side:

就我而言,我无法控制服务器设置,但我知道它期望“内容类型”为“应用程序/json”。我在 iOS 客户端这样做:

manager.requestSerializer = [AFJSONRequestSerializer serializer];

refer to AFNetworking version 2 content-type error

参考 AFNetworking 版本 2 内容类型错误