GET请求
- (IBAction)get:(id)sender {
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 *manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer =[AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingMutableLeaves];
[manager GET:urlString
parameters:nil
progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"请求成功");
NSLog(@"%@", responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"请求失败");
}];
}
POST请求
- (IBAction)post:(id)sender {
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";
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSDictionary *parameters = @{@"cinema_id" : @1533};
[manager POST:urlString
parameters:parameters
progress:nil
success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"请求成功");
NSLog(@"%@", responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"请求失败");
}];
}
通过POST请求发送带图文的微博
- (IBAction)postWeibo:(id)sender {
NSString *urlString = @"https://upload.api.weibo.com/2/statuses/upload.json";
NSString *token = @"2.00hd363CtKpsnBedca9b3f35tBYiPD";
NSDictionary *parameters = @{@"access_token" : token,
@"status" : @"这不是lip"
};
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager POST:urlString
parameters:parameters
constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
UIImage *image = [UIImage imageNamed:@"风景.jpg"];
NSData *imageData = UIImageJPEGRepresentation(image, 1);
[formData appendPartWithFileData:imageData name:@"pic" fileName:@"ljp" mimeType:@"image/jpeg"];
}
progress:^(NSProgress * _Nonnull uploadProgress) {
NSLog(@"%.1lf", (CGFloat)uploadProgress.completedUnitCount/uploadProgress.totalUnitCount*100);
}
success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"上传成功");
}
failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"上传失败");
}];
}