【发布时间】: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