• iOS开发之网络基础知识


    iOS开发之网络基础知识

    1. 一次HTTP请求的完整过程

      (1)浏览器或应用发起HTTP请求, 请求包含HTTP请求(GET,POST), 地址(URL), 协议(HTTP/1.1), 请求头部, 附加信息

      (2)web服务接收到请求, 进行处理, 页面分为两种页面(静态html页面, 动态php,asp,jsp页面)

      (3)数据通过响应response返回给浏览器和应用

      GET请求和POST的区别(面试题常考)

        (1)都是HTTP请求的方式, GET一般只是为了获取数据, 而POST获取的同时上传附加数据

        (2)GET所有参数都在URL网址中, POST请求参数放在请求头中

        (3)应用: 只是简单地获取数据, 使用GET请求,   登录(传密码), 上传文件或图片, 使用POST请求

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
        //1.
    //    [self testGetRequest];
        
        //2.
    //    [self testpostRequest];
        
        //3.上传文件(上传图片)
    //    [self testUploadFile];
        
        //4.下载文件
    //    [self testDownloadFile];
        
        //5.监控网络状态
        [self testMonitorNetworkStatus];
        
    }
    
    -(void)testMonitorNetworkStatus
    {
        AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"www.baidu.com"]];
        [manager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
            
            NSDictionary *dic = @{@(AFNetworkReachabilityStatusUnknown): @"未知",@(AFNetworkReachabilityStatusNotReachable): @"不可达",@(AFNetworkReachabilityStatusReachableViaWWAN): @"GPRS",@(AFNetworkReachabilityStatusReachableViaWiFi): @"Wifi"};
            NSLog(@"状态为 %@",dic[@(status)]);
            
        }];
        [manager.reachabilityManager startMonitoring];
        
    }
    
    -(void)testDownloadFile
    {
        NSString *urlString = @"ttp://imgcache.qq.com/club/item/avatar/zip/7/i87/all.zip";
        
        //创建会话管理器对象(通过默认配置)
        AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
        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];
        
    }
    
    -(void)testUploadFile
    {
        NSString *urlString = @"http://quiet.local/uploadtest/upload.php";
        
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        [manager POST:urlString parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
            
            NSString *path = [[NSBundle mainBundle] pathForResource:@"http://quiet.local/uploadtest/pk.php" ofType:nil];
            [formData appendPartWithFileURL:[NSURL fileURLWithPath:path] name:@"image1" fileName:@"10_5.jpg" mimeType:@"image/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);
        }];
        
    }
    
    -(void)testpostRequest
    {
        NSString *urlString = @"http://quiet.local/testdir/login.php";
        
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        
        [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);
        }];
        
    }
    
    -(void)testGetRequest
    {
        NSString *urlString = @"http://www.baidu.com";
        
        //定义AFHTTPRequestOperationManager管理
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        //注意:默认认为数据是JSON,content-type也是JSON
        //解决:设置解析器为HTTP形式,下载回来是NSData
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        [manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            
            //responseObject重要参数,包含下载数据
    //        NSLog(@"o = %@",responseObject);
            NSString *str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
            NSLog(@"str = %@",str);
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"error = %@",error);
        }];
        
    }
  • 相关阅读:
    local http
    redis 存储时间区间的数据
    json 和 jsonp
    ssdb 常用命令行
    php 冒泡排序
    pdo 函数
    异步的消息队列
    php 全局变量$_SERVER
    php 技能树
    get_called_class/get_class方法
  • 原文地址:https://www.cnblogs.com/SuperHanks/p/4399194.html
Copyright © 2020-2023  润新知