只适用于POST的网络请求

  • (void) networkManager : (NSString *)urlString
    parameter: (NSDictionary *)paraDic
    success:(void(^)(id obj)) success
    fail:(void(^)(NSError *error))fail
    {
    // NSURLSession 配置
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    //创建 sessionManager
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
    //创建请求
    NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@”POST” URLString:urlString parameters:paraDic constructingBodyWithBlock:nil error:nil];
    //创建请求任务
    NSURLSessionDataTask dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
    //如果有 error , 请求失败
    //反之, 请求成功
    if (error) {
    fail(error);
    } else {
    success(responseObject);
    }
    }];
    //开启任务
    [dataTask resume];
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章