1.GET请求
NSString *urlString = @"http://piao.163.com/m/cinema/list.html?app_id=1&mobileType=iPhone&ver=2.6&channel=appstore&deviceId=9E89CB6D-A62F-438C-8010-19278D46A8A6&apiVer=6&city=110000";
AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];
manger.requestSerializer = [AFHTTPRequestSerializer serializer];
manger.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingMutableLeaves];
[manger GET:urlString
parameters:nil
progress:^(NSProgress * _Nonnull downloadProgress) {
NSLog(@"%lli/%lli", downloadProgress.completedUnitCount, downloadProgress.totalUnitCount);
}
success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"请求成功");
NSLog(@"responseObject%@", responseObject);
}
failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"请求失败");
}];
2.POST请求
(1)普通POST请求
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSString *urlString = @"http://piao.163.com/m/cinema/schedule.html?app_id=1&mobileType=iPhone&ver=2.6&channel=appstore&deviceId=9E89CB6D-A62F-438C-8010-19278D46A8A6&apiVer=6&city=110000";
NSDictionary *parameters = @{@"cinema_id" : @"1533"};
[manager POST:urlString
parameters:parameters
progress:nil
success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"请求成功:%@", responseObject);
}
failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"请求失败");
}];
(2)微博接口,multipart/form-data POST文件上传
NSString *urlString = @"https://upload.api.weibo.com/2/statuses/upload.json";
NSString *token = @"2.00hd363CtKpsnBedca9b3f35tBYiP";
NSDictionary *dic = @{@"access_token" : token, @"status" : @"明天高考! "};
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager POST:urlString
parameters:dic
constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
UIImage *image = [UIImage imageNamed:@"1.jpg"];
NSData *data = UIImageJPEGRepresentation(image, 0.7);
[formData appendPartWithFileData:data name:@"pic" fileName:@"1.jpg" mimeType:@"image/jpeg"];
}
progress:^(NSProgress * _Nonnull downloadProgress) {
NSLog(@"%lli/%lli", downloadProgress.completedUnitCount, downloadProgress.totalUnitCount);
}
success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"上传成功");
}
failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"上传失败");
}];
3.下载任务
NSString *urlString = @"http://218.76.27.57:8080/chinaschool_rs02/135275/153903/160861/160867/1370744550357.mp3";
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
NSLog(@"%lli/%lli", downloadProgress.completedUnitCount, downloadProgress.totalUnitCount);
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
NSLog(@"状态码:%li", ((NSHTTPURLResponse *)response).statusCode);
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/菊花台.mp3"];
NSLog(@"%@", filePath);
return [NSURL fileURLWithPath:filePath];
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
NSLog(@"下载完成");
}];
[downloadTask resume];
[_progressView setProgressWithDownloadProgressOfTask:downloadTask animated:YES];
4.上传任务 multipart/form-data POST文件上传
NSString *urlString = @"https://api.weibo.com/2/statuses/upload.json";
NSString *token = @"2.00hd363CtKpsnBedca9b3f35tBYiP";
UIImage *image = _imageView.image;
NSString *text = _textField.text;
if (image == nil || text.length == 0) {
return;
}
NSDictionary *dic = @{@"access_token" : token,
@"status" : text};
AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer];
NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:urlString parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
NSData *imageData = UIImageJPEGRepresentation(image, 1);
[formData appendPartWithFileData:imageData name:@"pic" fileName:@"image.png" mimeType:@"image/png"];
} error:nil];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSURLSessionUploadTask *uploadTask;
uploadTask = [manager uploadTaskWithStreamedRequest:request progress:^(NSProgress * _Nonnull uploadProgress) {
dispatch_async(dispatch_get_main_queue(), ^{
[_progressView setProgress:uploadProgress.fractionCompleted];
});
} completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
NSLog(@"上传结束,状态码:%li", ((NSHTTPURLResponse *)response).statusCode);
}];
[uploadTask resume];