【发布时间】:2016-04-16 15:04:24
【问题描述】:
这是我第一次在我的应用程序中使用AFNetworking。我已经使用 pods 安装了最新版本(3.0)。现在的问题是 url 在浏览器和邮递员中运行良好,但是当我在应用程序中尝试时,它给了我未经授权的错误。不知道怎么授权。我在标题中添加用户名和密码。有谁能够帮我。
下面是我的代码。
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager.requestSerializer setValue:username forHTTPHeaderField:@"username"];
[manager.requestSerializer setValue:password forHTTPHeaderField:@"password"];
[manager GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
success(responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
failure(error);
}];
以下是完整的错误日志
Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: unauthorized (401)"
UserInfo={NSUnderlyingError=0x7fa1c3f945b0 {Error
Domain=com.alamofire.error.serialization.response Code=-1016 "Request
> failed: unacceptable content-type: text/html"
> UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse:
0x7fa1c3d139f0> { URL:
http://192.168.0.111/guesswhat/getcategories.json } { status code:
401, headers {
"Cache-Control" = "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";
Connection = "Keep-Alive";
"Content-Length" = 89;
"Content-Type" = "text/html";
Date = "Mon, 11 Jan 2016 12:30:55 GMT";
Expires = "Thu, 19 Nov 1981 08:52:00 GMT";
"Keep-Alive" = "timeout=5, max=99";
Pragma = "no-cache";
Server = "Apache/2.4.10 (Win32) OpenSSL/1.0.1i PHP/5.5.19";
"Www-Authenticate" = "Digest realm=\"Restricted area\",qop=\"auth\",nonce=\"5693a07f60c93\",opaque=\"cdce8a5c95a1427d74df7acbf41c9ce0\"";
Blockquote "X-Powered-By" = "PHP/5.5.19"; } },
【问题讨论】:
-
请发布错误控制台日志,以便我们找到解决方案。
-
好像您没有访问权限或纯凭据不正确
-
重新检查
username/password的值。 -
用户名/密码正确。它在邮递员中工作。
-
尝试打印 [[NSString alloc] initWithData:[error.userInfo objectForKey:AFNetworkingOperationFailingURLResponseDataErrorKey] encoding:4]
标签: ios objective-c api afnetworking