AFNetworking 将 json 响应读取为 text/html

问题描述

我有 2 个项目,一个用于用户,另一个用于提供者。

我对活动使用相同的代码和 sam api,它在提供程序中运行良好,但对用户不起作用!!

我用过这个代码

AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
    Nsstring *strURL=[Nsstring stringWithFormat:@"%@%@",SERVICE_URL,path];

manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];

错误是:

Error Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request Failed: forbidden (403)" UserInfo={NSLocalizedDescription=Request Failed: forbidden (403),NSUnderlyingError=0x600002a08ba0 {Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request Failed: unacceptable content-type: text/html" UserInfo={NSLocalizedDescription=Request Failed: unacceptable content-type: text/html,NSErrorFailingURLKey=https://api.com,com.alamofire.serialization.response.error.data={length = 3273,bytes = 0x3c21444f 43545950 45206874 6d6c3e0a ... 3c2f6874 6d6c3e0a },com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x6000024d0b80> { URL: https://api.com } { Status Code: 403,Headers {
    "Cache-Control" =     (
        "private,max-age=0,no-store,no-cache,must-revalidate,post-check=0,pre-check=0"
    );
    "content-encoding" =     (
        br
    );
    "Content-Type" =     (
        "text/html; charset=UTF-8"
    );
    Date =     (
        "Thu,25 Feb 2021 07:26:49 GMT"
    );
    Expires =     (
        "Thu,01 Jan 1970 00:00:01 GMT"
    );

我在代码添加了这一行:

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

但是出现这个错误

JSON 文本未以数组或对象开头,并且没有设置允许片段的选项。

然后,我添加了以下代码

manager.responseSerializer = [AFJSONResponseSerializer
serializerWithReadingOptions:NSJSONReadingallowFragments];

它给了我同样的问题来接受 text/html !!!

请给我一个解决方案。

先谢谢你。

解决方法

修复了!!

我尝试使用此代码打印错误:

NSString* ErrorResponse = [[NSString alloc] initWithData:(NSData *)error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] encoding:NSUTF8StringEncoding];

NSLog(@"%@",ErrorResponse);

错误是: Access denied | api.com used Cloudflare to restrict access

The owner of this website (api.com) has banned your access based on your browser's signature

这是一个服务器安全问题。