AFHTTPRequestOperationManager 获取请求失败,错误域=AFNetworkingErrorDomain Code=-1016
Posted
技术标签:
【中文标题】AFHTTPRequestOperationManager 获取请求失败,错误域=AFNetworkingErrorDomain Code=-1016【英文标题】:AFHTTPRequestOperationManager Get request failed,Error Domain=AFNetworkingErrorDomain Code=-1016 【发布时间】:2014-01-15 11:30:16 【问题描述】:我向这样的 url 发送 GET 请求:https://test.com/app/login?json="user":"test@gmail.com","password":"test"
但返回此错误:
Error Domain=AFNetworkingErrorDomain Code=-1016 "Request failed: unacceptable content-type: text/plain" UserInfo=0x9ad3520 NSErrorFailingURLKey=https://test.com/app/login?json=%7B%22user%22:%22test@gmail.com%22,%22password%22:%22test%22%7D, AFNetworkingOperationFailingURLResponseErrorKey= URL: https://test.com/app/login?json=%7B%22user%22:%22test@gmail.com%22,%22password%22:%22test%22%7D status code: 200,
headers
"Cache-Control" = "max-age=900";
Connection = "Keep-Alive";
"Content-Length" = 6116;
"Content-Type" = "text/plain; charset=UTF-8";
Date = "Wed, 15 Jan 2014 09:14:22 GMT";
Expires = "Wed, 15 Jan 2014 09:29:22 GMT";
"Keep-Alive" = "timeout=2, max=100";
Server = "Apache-Coyote/1.1";
"Set-Cookie" = "APP-SESSION-ID=xxxxxxxxxxxxxxxx; Path=/app/; HttpOnly, APP-ROUTE-ID=.app1_node2; path=/, API-ROUTE-ID=.app1_node2; path=/";
, NSLocalizedDescription=Request failed: unacceptable content-type: text/plain
这是我的代码,我使用 NSUTF8StringEncoding 添加了百分比转义。
manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)
failure:^(AFHTTPRequestOperation *operation, NSError *error)
];
【问题讨论】:
将此作为答案并自己接受 @Lithu T.V 感谢您的提醒。 【参考方案1】:我已经解决了这个问题。
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
参考:github.com/AFNetworking/AFNetworking/issues/1586
【讨论】:
我使用了您的代码,并得到其他错误代码=3840。我检查的主机也有 api 响应 json 类型和文本类型。这会导致错误吗?谢谢以上是关于AFHTTPRequestOperationManager 获取请求失败,错误域=AFNetworkingErrorDomain Code=-1016的主要内容,如果未能解决你的问题,请参考以下文章