【问题标题】:AFNetworking read json response as text/htmlAFNetworking 将 json 响应读取为 text/html
【发布时间】:2021-05-27 13:49:08
【问题描述】:

我有 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 !!!

请给我一个解决方案。

提前谢谢你。

【问题讨论】:

  • 我注意到你有一个Request failed: forbidden (403),我认为问题更多在于403,不是吗?你能检索响应数据并将其转换为字符串吗?
  • @Larme 响应数据始终为空!并且函数返回错误和失败。什么请求失败:禁止(403)是为了什么?
  • 如果stackoverflow.com/questions/35083273/… 出现错误,请尝试检索响应正文,它应该提供信息。其余的,请阅读 HTTP 403 错误代码。
  • @Larme 它给了我一个空的 NSLog!没有显示
  • 我看到了com.alamofire.serialization.response.error.data={length = 3273, bytes = 0x3c21444f 43545950 45206874 6d6c3e0a ... 3c2f6874 6d6c3e0a 的信息,尝试获取该数据。所以我会说:NSData *errorData = error.userInfo[@"com.alamofire.serialization.response.error.data"],如果不为零,则使用stackoverflow.com/questions/1305225/… 将其转换为十六进制字符串。

标签: objective-c xcode afnetworking


【解决方案1】:

解决了!!

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-23
    • 2019-09-30
    • 2015-07-02
    • 1970-01-01
    • 1970-01-01
    • 2013-03-11
    • 2014-02-28
    • 1970-01-01
    相关资源
    最近更新 更多