新版AFN快速发送GET、POST请求

由于之前看视频学习资料都用的是14年之前AFNetworking框架,和最新的有很大不同,为了方便大家使用,我列出了最基本的POST,GET请求方法。

GET: 

     AFHTTPRequestOperationManager  *manager = [AFHTTPRequestOperationManager manager];
[manager 
GET : @"http://example.com/resources.json"   parameters : nil   success :^( AFHTTPRequestOperation  *operation,  id  responseObject) {
     
NSLog ( @"JSON: %@" , responseObject);
     } 
failure :^( AFHTTPRequestOperation  *operation,  NSError  *error) {
     
NSLog ( @"Error: %@" , error);

     }];


POST :

     AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

      NSDictionary  *parameters =  @{ @"foo" @"bar" } ;
[manager 
POST : @"http://example.com/resources.json"   parameters :parameters  success :^( AFHTTPRequestOperation  *operation,  id  responseObject) {
     
NSLog ( @"JSON: %@" , responseObject);
     } 
failure :^( AFHTTPRequestOperation  *operation,  NSError  *error) {
     
NSLog ( @"Error: %@" , error);

     }];


POST(Multi-Part Request) :

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManagermanager];

   NSDictionary *parameters = @{@"foo": @"bar"};

    NSURL *filePath = [NSURLfileURLWithPath:@"file://path/to/image.png"];

    [managerPOST:@"http://example.com/resources.json"parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

        [formDataappendPartWithFileURL:filePath name:@"image" error:nil];

    }success:^(AFHTTPRequestOperation *operation,id responseObject) {

       NSLog(@"Success: %@", responseObject);

    }failure:^(AFHTTPRequestOperation *operation,NSError *error) {

       NSLog(@"Error: %@", error);

    }];


创建下载任务(download):  

    NSURLSessionConfiguration *configuration = [NSURLSessionConfigurationdefaultSessionConfiguration];

    AFURLSessionManager *manager = [[AFURLSessionManageralloc] initWithSessionConfiguration:configuration];

    

    NSURL *URL = [NSURLURLWithString:@"http://example.com/download.zip"];

   NSURLRequest *request = [NSURLRequestrequestWithURL:URL];

    

   NSURLSessionDownloadTask *downloadTask = [managerdownloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath,NSURLResponse *response) {

        NSURL *documentsDirectoryURL = [[NSFileManagerdefaultManager] URLForDirectory:NSDocumentDirectoryinDomain:NSUserDomainMaskappropriateForURL:nilcreate:NOerror:nil];

       return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];

    }completionHandler:^(NSURLResponse *response,NSURL *filePath, NSError *error) {

        NSLog(@"File downloaded to: %@", filePath);

    }];

    [downloadTaskresume];


创建上传任务(upload):

    NSURLSessionConfiguration *configuration = [NSURLSessionConfigurationdefaultSessionConfiguration];

    AFURLSessionManager *manager = [[AFURLSessionManageralloc] initWithSessionConfiguration:configuration];

    

    NSURL *URL = [NSURLURLWithString:@"http://example.com/upload"];

   NSURLRequest *request = [NSURLRequestrequestWithURL:URL];

    

    NSURL *filePath = [NSURLfileURLWithPath:@"file://path/to/image.png"];

   NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithRequest:request fromFile:filePathprogress:nilcompletionHandler:^(NSURLResponse *response,id responseObject, NSError *error) {

       if (error) {

           NSLog(@"Error: %@", error);

        }else {

           NSLog(@"Success: %@ %@", response, responseObject);

        }

    }];

    [uploadTaskresume];


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值