AFNetworking请求失败的问题

时间:2015-08-18 09:52:08

标签: ios iphone afnetworking

我遇到了无法连接到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 个答案:

答案 0 :(得分:0)

状态代码为401,表示您的帐户或密码不正确。我建议您应该注销params,并找出它们是否为null或其他任何东西