浏览器/应用 发出请求(请求包含HTTP请求(GET,POST),地址URL,协议(HTTP/1.1),请求头部,附加信息)
--> Web服务端处理(页面:静态html页面、动态php/asp/jsp页面) --> 浏览器/应用
GET/POST区别:
都是HTTP请求方式
GET:只获取数据,所有参数都在URL网址中,用于简单地获取数据(不安全)
POST:获取上传附加数据,请求参数在请求头文件,需传输加密信息
// [self testGetRequest]; //1.GET请求(html,json,xml) // [self testPostRequest]; //2.POST请求 // [self testUploadFile]; //3.上传文件 // [self testDownloadFile]; //4.下载文件 [self testMonitorNetworkStatus]; //5.监控网络状态 //6.图片异步功能(替代SDWebImage) // UIKit+AFNetworking.h // - (void)setImageWithURL:(NSURL *)url; //7.有些请求特殊的请求头 // BAIDU_WISE_UID=wapp_1428385381699_466; //AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; //[manager.requestSerializer setValue:@"wapp_1428385381699_466" forHTTPHeaderField:@"BAIDU_WISE_UID"];
1.GET请求
#pragma mark - 1.GET请求 -(void)testGetRequest{ NSString *urlStr = @"http://www.baidu.com"; //定义AFHTTPRequest管理对象 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; //注:默认情况下,默认数据格式JSON,content-type:JSON ,responseObjectd是解析的字典和数组 //不是产生错误Code=-1016 //解决:设置解析器为HTTP形式,下载回来的为NSData manager.responseSerializer = [AFHTTPResponseSerializer serializer]; [manager GET:urlStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { //responseObject 重要参数--> 包含下载的数据 NSString *str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]; NSLog(@"str = %@",str); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"error %@",error); }]; }
2.POST请求
#pragma mark - 2.POST请求 -(void)testPostRequest{ //POST接口:http://quiet.local/testdir/login.php //参数1: @"user" : @"quiet" //参数2: @"password" : @"123" NSString *urlString = @"http://quiet.local/testdir/login.php"; AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.requestSerializer = [AFHTTPRequestSerializer serializer]; //参数1: 传入地址 //参数2: 传入URL请求的参数, 格式传入字典//parameters --> 传进一个字典@{} [manager POST:urlString parameters:@{@"user":@"quiet",@"password":@"123"} success:^(AFHTTPRequestOperation *operation, id responseObject) { NSString *str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]; NSLog(@"str = %@",str); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"error %@",error); }]; }
3.图片上传
#pragma mark - 3.图片上传 -(void)testUploadFile{ //POST上传接口 NSString *urlStr = @"http://quiet.local/uploadtest/upload.php"; //参数名: image : 参数值是图片 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.responseSerializer = [AFHTTPResponseSerializer serializer]; [manager POST:urlStr parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { //实现:上传的数据附加到请求体中 NSString *path = [[NSBundle mainBundle] pathForResource:@"back2.jpg" ofType:nil]; //mimeType 多用途互联网邮件扩展类型, 每种文件都有一个类型 --> 直接上网搜格式 [formData appendPartWithFileURL:[NSURL fileURLWithPath:path] name:@"12222" fileName:@"mddse.jpg" mimeType:@"12222/jpeg" error:nil]; } success:^(AFHTTPRequestOperation *operation, id responseObject) { NSString *str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]; NSLog(@"str = %@",str); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"error = %@",error); }]; }
4.下载文件
#pragma mark - 4.下载文件 -(void)testDownloadFile{ NSString *urlString = @"http://imgcache.qq.com/club/item/avatar/zip/7/i87/all.zip"; //创建回话管理器对象(默认配置)AFURLSessionManager *manager AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; // NSURLSessionDownloadTask *task NSURLSessionDownloadTask *task = [manager downloadTaskWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]] progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) { //返回文件保存的位置 NSString *path = [NSString stringWithFormat:@"%@/Documents/all.zip",NSHomeDirectory()]; NSLog(@"path = %@",path); return [NSURL fileURLWithPath:path]; } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) { // NSLog(@"下载完成"); }]; // [task resume]; //启动任务 }
5.监控网络状态
#pragma mark - 5.监控网络状态 -(void)testMonitorNetworkStatus{ AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"www.baidu.com"]]; // [manager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { //AFNetworkReachabilityStatusn几种状态 NSDictionary *dict =@{@(AFNetworkReachabilityStatusUnknown):@"未知",@(AFNetworkReachabilityStatusNotReachable):@"不可达",@(AFNetworkReachabilityStatusReachableViaWWAN):@"GPRS",@(AFNetworkReachabilityStatusReachableViaWiFi):@"Wifi"}; NSLog(@"状态为 %@",dict[@(status)]); }]; [manager.reachabilityManager startMonitoring]; //开启状态监视 }