#import "ViewController.h"
#import <AFNetworking.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
get請求方式
//創(chuàng)建AFHTTPSessionManager對象
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//響應體序列化對象
// AFHTTPResponseSerializer *noSerializer = [AFHTTPResponseSerializer serializer]; //二進制的形式 (不解析)
// AFJSONResponseSerializer *jsonSerializer = [AFJSONResponseSerializer serializer]; //json的格式 (默認)
AFXMLParserResponseSerializer *xmlSerializer = [AFXMLParserResponseSerializer serializer];//xml格式
// AFImageResponseSerializer *imageSerializer = [AFImageResponseSerializer serializer]; //image格式
manager.responseSerializer = xmlSerializer;
//(2)請求體序列化對象
//請求體中的數(shù)據(jù)格式
AFJSONRequestSerializer *jsonRequestSer =[AFJSONRequestSerializer serializer]; //json格式
//設置請求體序列化
manager.requestSerializer = jsonRequestSer;
//設置get請求方式
[manager GET:@"http://120.25.226.186:32812/login?username=520it&pwd=520it" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"1%@",responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"2%@",error);
}];
post請求方式一
//創(chuàng)建一個參數(shù)字典
NSDictionary *dict = @{@"access_token":@"2.00ZyBPBGLWzBDDef60fc6737ygaudC",@"id":@"4008406383515844"};
//創(chuàng)建AFHTTPSessionManager對象
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//創(chuàng)建post申請方式
[manager POST:@"https://api.weibo.com/2/statuses/destroy.json" parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
//返回響應對象
NSLog(@"-------responseObject=%@",responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
post請求方式二
//設置請求路徑
NSString *url = @"https://upload.api.weibo.com/2/statuses/upload.json";
//設置參數(shù)字典
NSDictionary *params = @{@"access_token":@"2.00ZyBPBGLWzBDDef60fc6737ygaudC",@"status":@"我們在學AFNetWorking..."};
//1創(chuàng)建AFHTTPSessionManager對象
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//2創(chuàng)建post申請方式
[manager POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
//fromData:生成請求體的data對象
//復雜數(shù)據(jù) 追加到formData中
//獲得圖片路徑
NSString *path = [[NSBundle mainBundle]pathForResource:@"1.jpeg" ofType:nil];
//獲得圖片data
NSData *picData = [NSData dataWithContentsOfFile:path];
//把圖片數(shù)據(jù)追加到formData中
[formData appendPartWithFileData:picData name:@"pic" fileName:@"a" mimeType:@"application/octet-stream"];//類型
} progress:^(NSProgress * _Nonnull uploadProgress) {
//上傳進度
NSLog(@"------uploadProgress%@",uploadProgress);
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"--------responseObject=%@",responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"--------error=%@",error);
}];
下載文件
- (void)download{
AFHTTPSessionManager *mannager = [AFHTTPSessionManager manager];
//下載路徑
NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/resources/videos/minion_01.mp4"];
//設置路徑請求
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//創(chuàng)建下載任務
NSURLSessionDownloadTask *task = [mannager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
//輸出下載進度
NSLog(@"-------downloadProgress=%@",downloadProgress);
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
//targetPath:返回一個下載文件的保存路徑
//response:返回一個響應頭
//創(chuàng)建文件保存的路徑
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/1.mp4"];
return [NSURL fileURLWithPath:path];
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
//response:返回一個響應頭
//filePath:最終保存的路徑
NSLog(@"filePath=%@",filePath);
//返回錯誤信息(下載失敗有值)
NSLog(@"error = %@",error);
}];
//恢復任務
[task resume];