- 首先利用 CocoaPods 把框架AFN導(dǎo)入到項目中扑媚。
- 然后在ViewController里面導(dǎo)入<AFNetworking.h>, http請求記得配置 APP Transport。
- 然后就可以具體實現(xiàn)了:
#pragma mark - get請求
-(void)get
{
// 創(chuàng)建會話管理者
AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
// 寫參數(shù)
NSDictionary *paramDict=@{
@"username":@"520it",
@"pwd":@"520it",
@"type":@"JSON"
};
// 發(fā)送get請求
[manager GET:@"http://120.25.226.186:32812/login" parameters:paramDict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"%@-%@",[responseObject class],responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"請求失敗--%@",error);
}];
}
#pragma mark - post請求
-(void)post
{
// 創(chuàng)建會話管理者
AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
// 寫參數(shù)
NSDictionary *paramDict=@{
@"username":@"520it",
@"pwd":@"520it",
@"type":@"JSON"
};
// 發(fā)送post請求
[manager POST:@"http://120.25.226.186:32812/login" parameters:paramDict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"%@-%@",[responseObject class],responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"請求失敗--%@",error);
}];
}
#pragma mark - 文件下載
- (void)download
{
// 創(chuàng)建會話管理者
AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
NSURL *url=[NSURL URLWithString:@"http://120.25.226.186:32812/resources/images/minion_01.png"];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
// 下載文件
NSURLSessionDownloadTask *download=[manager downloadTaskWithRequest: request
progress:^(NSProgress * _Nonnull downloadProgress) {
// 監(jiān)聽下載進度
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(@"targetPath:%@",targetPath);
NSLog(@"fullPath:%@",fullPath);
return [NSURL fileURLWithPath:fullPath];
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
NSLog(@"%@",filePath);
}];
[download resume];
}
#pragma mark - 文件上傳
- (void)upload
{
// 1. 創(chuàng)建會話管理者
AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
// 2. 發(fā)送POST請求上傳文件
/**
第一個參數(shù):請求路徑
第二個參數(shù):字典(非文件參數(shù))
第三個參數(shù):constructingBodyWithBlock 處理要上傳的文件數(shù)據(jù)
第四個參數(shù):進度回調(diào)
第五個參數(shù):成功回調(diào) responseObject:響應(yīng)體信息
第六個參數(shù):失敗回調(diào)
*/
[manager POST:@"http://120.25.226.186:32812/upload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
UIImage *image=[UIImage imageNamed:@"aaa"];
NSData *imageData=UIImagePNGRepresentation(image);
//使用formData來拼接數(shù)據(jù)
/*
第一個參數(shù):二進制數(shù)據(jù) 要上傳的文件參數(shù)
第二個參數(shù):服務(wù)器規(guī)定的
第三個參數(shù):該文件上傳到服務(wù)器以什么名稱保存
*/
[formData appendPartWithFileURL:[NSURL fileURLWithPath:@"/Users/xzq/Desktop/aaa.png"] 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);
}];
}