Get
#import "ViewController.h"
#import "AFNetworking.h"
@interface ViewController ()
@end
@implementation ViewController
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self get];
}
//發(fā)送get請(qǐng)求
-(void)get
{
//1.創(chuàng)建會(huì)話管理者
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//2.發(fā)送請(qǐng)求
/*
第一個(gè)參數(shù):請(qǐng)求路徑
第二個(gè)參數(shù):字典
第三個(gè)參數(shù):progress 進(jìn)度
第四個(gè)參數(shù):success 成功回調(diào)
task :請(qǐng)求任務(wù) task.response
responseObject:響應(yīng)體信息(內(nèi)部已經(jīng)完成了序列化處理.JSON)
第五個(gè)參數(shù):failure 失敗的回調(diào)
error:錯(cuò)誤信息
*/
[manager GET:@"http://120.25.226.186:32812/video" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
//3.解析
NSLog(@"%@--%@",[responseObject class],responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
}
@end

31-15.gif
Post
#import "ViewController.h"
#import "AFNetworking.h"
@interface ViewController ()
@end
@implementation ViewController
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self post];
}
//發(fā)送post請(qǐng)求
/*
1)設(shè)置請(qǐng)求方法為POST
2)把參數(shù)設(shè)置在請(qǐng)求體里面
*/
-(void)post
{
//1.創(chuàng)建會(huì)話管理者
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//創(chuàng)建字典,轉(zhuǎn)載參數(shù)
NSDictionary *dictM =@{
@"username":@"Zb",
@"pwd":@"CoderZb",
@"type":@"JSON"
};
//2.發(fā)送請(qǐng)求
/*
第一個(gè)參數(shù):請(qǐng)求路徑
第二個(gè)參數(shù):字典
第三個(gè)參數(shù):progress 進(jìn)度
第四個(gè)參數(shù):success 成功回調(diào)
task :請(qǐng)求任務(wù) task.response
responseObject:響應(yīng)體信息(內(nèi)部已經(jīng)完成了序列化處理.JSON)
第五個(gè)參數(shù):failure 失敗的回調(diào)
error:錯(cuò)誤信息
*/
[manager POST:@"http://120.25.226.186:32812/video" parameters:dictM progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
//3.解析
NSLog(@"%@--%@",[responseObject class],responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
}
@end

31-16.gif
UpLoad
#import "ViewController.h"
#import "AFNetworking.h"
@interface ViewController ()
@end
@implementation ViewController
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self upload];
}
//文件上傳
-(void)upload
{
//1.創(chuàng)建會(huì)話管理者
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//2.發(fā)送POST請(qǐng)求上傳文件
/*
第一個(gè)參數(shù):請(qǐng)求路徑 NSString
第二個(gè)參數(shù):非文件參數(shù)
第三個(gè)參數(shù):constructingBodyWithBlock 用來(lái)拼接要上傳的文件數(shù)據(jù)
第四個(gè)參數(shù):progress進(jìn)度回調(diào)
第五個(gè)參數(shù):success 上傳成功的回調(diào)
第六個(gè)參數(shù):failure 失敗后的回調(diào)
*/
[manager POST:@"http://120.25.226.186:32812/upload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
//方法1:
//拼接要上傳的文件數(shù)據(jù)
/*
第一個(gè)參數(shù):要上傳的文件的二進(jìn)制數(shù)據(jù)
第二個(gè)參數(shù):參數(shù)名稱 file
第三個(gè)參數(shù):該文件上傳到服務(wù)器之后應(yīng)該以什么名稱來(lái)保存
第四個(gè)參數(shù):文件的MIMEType
*/
/*
UIImage *image= [UIImage imageNamed:@"100.7.png"];
NSData *imageData = UIImagePNGRepresentation(image);
[formData appendPartWithFileData:imageData name:@"file" fileName:@"haha.png" mimeType:@"image/png"];//加載本地的圖片
*/
//方法2:
//FileURL:要上傳的文件的URL路徑
/*
NSURL *fileUrl = [NSURL fileURLWithPath:@"/Users/zhangbin/Desktop/100.7.png"];//加載指定路徑上的圖片
[formData appendPartWithFileURL:fileUrl name:@"file" fileName:@"1233.png" mimeType:@"image/png" error:nil];
*/
//方法3
//該方法是簡(jiǎn)單版本,該方法內(nèi)部會(huì)自動(dòng)的得到文件的名稱以及文件的類型
NSURL *fileUrl = [NSURL fileURLWithPath:@"/Users/zhangbin/Desktop/100.7.png"];//加載指定路徑上的圖片
[formData appendPartWithFileURL:fileUrl name:@"file" error:nil];
} progress:^(NSProgress * _Nonnull uploadProgress) {
NSLog(@"%f",1.0 * uploadProgress.completedUnitCount / uploadProgress.totalUnitCount);
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"%@",responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"失敗---%@",error);
}];
}
@end

31-18.gif
DownLoad
#import "ViewController.h"
#import "AFNetworking.h"
@interface ViewController ()
@end
@implementation ViewController
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self download];
}
//文件下載
-(void)download
{
//1.創(chuàng)建會(huì)話管理者
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//2.創(chuàng)建請(qǐng)求對(duì)象
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://120.25.226.186:32812/resources/images/minion_04.png"]];
//3.創(chuàng)建下載任務(wù)
/*
第一個(gè)參數(shù):請(qǐng)求對(duì)象
第二個(gè)參數(shù):進(jìn)度回調(diào) downloadProgress(獲得進(jìn)度信息)
第三個(gè)參數(shù):destination
targetPath:文件的臨時(shí)存儲(chǔ)路徑
response:響應(yīng)頭信息
返回值:NSURL(AFN內(nèi)部已經(jīng)實(shí)現(xiàn)了文件剪切的過(guò)程,但是需要我們告訴他應(yīng)該把文件剪切到哪里)
第四個(gè)參數(shù):completionHandler 請(qǐng)求完成的時(shí)候調(diào)用
response:響應(yīng)頭信息
filePath==fullPath 文件的最終目錄
error:錯(cuò)誤信息
*/
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
NSLog(@"%f",1.0 *downloadProgress.completedUnitCount /downloadProgress.totalUnitCount);
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
NSString *fullPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]stringByAppendingPathComponent:response.suggestedFilename];
NSLog(@"---%@---\n%@",targetPath,fullPath);
return [NSURL fileURLWithPath:fullPath];
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
NSLog(@"下載完成");
}];
//4.執(zhí)行方法
[downloadTask resume];
}
@end
31-17.gif

31-17.gif