AFN请求

作者: 路这么长 | 来源:发表于2016-11-23 17:02 被阅读14次


#### Creating an Upload Task

```objective-c
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

NSURL *URL = [NSURL URLWithString:@"http://example.com/upload"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];

NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];
NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithRequest:request fromFile:filePath progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    if (error) {
        NSLog(@"Error: %@", error);
    } else {
        NSLog(@"Success: %@ %@", response, responseObject);
    }
}];
[uploadTask resume];

Creating an Upload Task for a Multi-Part Request, with Progress

NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://example.com/upload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileURL:[NSURL fileURLWithPath:@"file://path/to/image.jpg"] name:@"file" fileName:@"filename.jpg" mimeType:@"image/jpeg" error:nil];
    } error:nil];

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

NSURLSessionUploadTask *uploadTask;
uploadTask = [manager
              uploadTaskWithStreamedRequest:request
              progress:^(NSProgress * _Nonnull uploadProgress) {
                  // This is not called back on the main queue.
                  // You are responsible for dispatching to the main queue for UI updates
                  dispatch_async(dispatch_get_main_queue(), ^{
                      //Update the progress view
                      [progressView setProgress:uploadProgress.fractionCompleted];
                  });
              }
              completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
                  if (error) {
                      NSLog(@"Error: %@", error);
                  } else {
                      NSLog(@"%@ %@", response, responseObject);
                  }
              }];

[uploadTask resume];

Creating a Data Task

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

NSURL *URL = [NSURL URLWithString:@"http://httpbin.org/get"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];

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

相关文章

  • AFN多请求依赖(二)

    导读:AFN多请求依赖(一) AFN多请求依赖(一)中解决的是如何将不同的AFN请求有序的进行请求:即第一个请求发...

  • (转)iOS 网络请求汇总

    原生session的GET请求 原生session的POST请求 AFN的GET网络请求如下: AFN POS...

  • iOS开发之AFNetworking框架

    AFN网络请求和文件上传下载 使用AFN框架处理网络数据请求,遇到以下报错: 首先AFN使用方法,都是从创建man...

  • 最简单的iOS网络请求

    做iOS开发,说到网络请求,大家可能都不约而同的提到AFN,可以说大家的网络请求都是用AFN封装而成,AFN的强大...

  • AFN请求

    // // ViewController.m // DOM解析 // // Created by on 2018/...

  • AFN请求

    Creating an Upload Task for a Multi-Part Request, with Pr...

  • iOS开发 AFN配置https请求

    iOS开发 AFN配置https请求

  • AFN2.x分析

    AFN2.x afn2.x 用单利AFHTTPRequestOperationManager对象请求manager...

  • iOS面试题收集(之AFN 网络请求)

    AFN:网络请求 AFN主要使用在2个场景中 (1):发送网络请求(2):实时监测网络状态 AFNetworkin...

  • iOS开发-AFNetworking框架基本使用

    一、AFN框架基本使用 1.1 AFN内部结构 1.2 AFN的基本使用 (1)发送POST请求的方式 (2)使用...

网友评论

      本文标题:AFN请求

      本文链接:https://www.haomeiwen.com/subject/aaevpttx.html