请求失败:未授权(401)AFNetwoking如何解决此问题

时间:2022-06-03 23:08:35
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSString *URLString = @"http://162.243.9.247:1337/login";
NSURL*url=[NSURL URLWithString:URLString];
NSDictionary *parameters = @{@"email": @"password", @"baz": @[@1, @2, @3]};
NSMutableURLRequest*request=[[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:URLString parameters:parameters error:nil];

manager.responseSerializer=[AFHTTPResponseSerializer serializer];

[[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:URLString parameters:parameters error:nil];

NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    if (error) {
        //NSLog(@"Error: %@", error);
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Error" message:error.localizedDescription preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
        [alertController addAction:ok];

        [self presentViewController:alertController animated:YES completion:nil];

    } else {

        NSArray *responseArray = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"%@ ----->%@", response, responseArray[0][@"lfs"]);
        self.fullarray = responseArray[0][@"lfs"];
    }
}];

[dataTask resume];

1 个解决方案

#1


0  

make sure

请求失败:未授权(401)AFNetwoking如何解决此问题

Step for server uploading

服务器上传步骤

  NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

 NSString *urlType = @"http://162.243.9.247:1337/login";
NSDictionary *param = @{@"email":@"one_user@gmail.com", @"password":@"asdfasdf"};

NSMutableURLRequest*request=[[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:urlType parameters:param error:nil];


[[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:URLString parameters:parameters error:nil];

NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    if (error) {
        //NSLog(@"Error: %@", error);

    } else {

        NSArray *responseArray = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"%@ ----->%@", response, responseArray[0][@"lfs"]);
        self.fullarray = responseArray[0][@"lfs"];
    }
}];

[dataTask resume];

#1


0  

make sure

请求失败:未授权(401)AFNetwoking如何解决此问题

Step for server uploading

服务器上传步骤

  NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

 NSString *urlType = @"http://162.243.9.247:1337/login";
NSDictionary *param = @{@"email":@"one_user@gmail.com", @"password":@"asdfasdf"};

NSMutableURLRequest*request=[[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:urlType parameters:param error:nil];


[[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:URLString parameters:parameters error:nil];

NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    if (error) {
        //NSLog(@"Error: %@", error);

    } else {

        NSArray *responseArray = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"%@ ----->%@", response, responseArray[0][@"lfs"]);
        self.fullarray = responseArray[0][@"lfs"];
    }
}];

[dataTask resume];