AFNetworking 请求失败问题
Posted
技术标签:
【中文标题】AFNetworking 请求失败问题【英文标题】:AFNetworking Request failing issue 【发布时间】:2015-08-18 09:52:08 【问题描述】:我遇到了无法连接到 API 的问题。这只发生在 ios8 上。它适用于iOS7。
- (void)performSearch:(NSString*)type : (NSString*)keyword
url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", API, keyword]];
request = [NSURLRequest requestWithURL:url];
operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
credential = [NSURLCredential credentialWithUser:USERNAME password:PASSWORD persistence:NSURLCredentialPersistenceForSession];
[operation setCredential:credential];
[[NSOperationQueue mainQueue] addOperation:operation];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
NSError *error = nil;
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&error];
if (error)
NSLog(@"Error serializing %@", error);
return;
[self parseJSON:JSON :keyword];
failure:^(AFHTTPRequestOperation *operation, NSError *error)
NSLog(@"error : %@", error);
];
[operation start];
我收到的错误消息。
Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: unauthorized (401)" UserInfo=0x7ff0650052f0 com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7ff062c5e4d0> URL: https://distribution.virk.dk/cvr-permanent-prod-20150622/_search?q=anders status code: 401, headers
Connection = "keep-alive";
"Content-Length" = 194;
"Content-Type" = "text/html";
Date = "Tue, 18 Aug 2015 09:36:09 GMT";
Server = "nginx/1.6.2";
"Www-Authenticate" = "Basic realm=\"Beskyttet adgang\"";
, NSErrorFailingURLKey=https://distribution.virk.dk/cvr-permanent-prod-20150622/_search?q=anders, com.alamofire.serialization.response.error.data=<3c68746d 6c3e0d0a 3c686561 643e3c74 69746c65 3e343031 20417574 686f7269 7a617469 6f6e2052 65717569 7265643c 2f746974 6c653e3c 2f686561 643e0d0a 3c626f64 79206267 636f6c6f 723d2277 68697465 223e0d0a 3c63656e 7465723e 3c68313e 34303120 41757468 6f72697a 6174696f 6e205265 71756972 65643c2f 68313e3c 2f63656e 7465723e 0d0a3c68 723e3c63 656e7465 723e6e67 696e782f 312e362e 323c2f63 656e7465 723e0d0a 3c2f626f 64793e0d 0a3c2f68 746d6c3e 0d0a>, NSLocalizedDescription=Request failed: unauthorized (401)
【问题讨论】:
【参考方案1】:状态码为 401,表示您的帐号或密码不正确。我建议您应该注销这些参数,并找出它们是否为空或其他任何东西
【讨论】:
以上是关于AFNetworking 请求失败问题的主要内容,如果未能解决你的问题,请参考以下文章
AFNetworking 识别失败原因:用户取消请求或网络故障
错误:请求失败:错误请求(400)通过afnetworking快速登录
AFNetworking 问题:NSErrorFailingURLKey=url,NSLocalizedDescription=请求失败:错误请求 (400)