AFNetworking实现网络请求
2016-06-06 20:02
232 查看
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"; //创建http会话管理对象 AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; //设置请求数据类型 //AFHTTPRequestSerializer key=value形式(默认) //AFJSONRequestSerializer JSON数据形式 manager.requestSerializer = [AFHTTPRequestSerializer serializer]; //设置接受数据类型 //AFHTTPResponseSerializer 不解析数据 //AFJSONResponseSerializer JSON解析 (默认) //AFXMLParserResponseSerializer XML解析 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); //name上传数据的参数名字 //mimeType:数据的类型名 //jpg -- image/jpeg //png -- image/png [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(@"上传失败"); }]; }
相关文章推荐
- Retrofit 2.0 超能实践(一),okHttp完美支持Https传输
- Retrofit 2.0 超能实践,完美支持Https传输
- Python 网络套接字编程
- Android HttpURLConnection的简单使用
- 网络资源
- HTTP协议报文、工作原理及Java中的HTTP通信技术详解
- socket与TCP/UDP编程
- http协议下的keep alive
- 腾讯兴趣部落可以发外部链接了
- 网站app被劫持怎么办?HTTPDNS阿里云域名防劫持, DNSPod 移动解析服务 D+
- 表单提交原理
- ios网络请求报错:The resource could not be loaded because the App Transport Security policy requires the us
- dumpheaders_http_threading_server.py脚本的Python 3.5迁移版
- HttpClient 4.4 请求
- ubuntu14.04 NFS环境的搭建
- The type org.apache.http.HttpResponse cannot be resolved. It is indirectly referenced from required
- 2016 ISCC CTF 比赛 WEB类第一题
- Google 深度学习笔记 卷积神经网络
- 【snmp】华为和H3C 网络设备设置snmp
- TensorFlow 深度学习笔记 卷积神经网络