• iOS进阶(网络请求)


    1.网络请求方式

    GET:通过网址字符串  网址字符串最多255字节  所有传输给服务器的数据都显示在网址里,直接可见,不安全

    POST:通过data  容量很大 数据被转换成二进制数 安全

    2.连接方式

    同步连接:程序容易出现卡死现象

    异步连接:等待数据返回 (有代理和block两种方式)创建请求对象时,采用NSMutableURLRequest对象并设置请求方式和body主体

    POSTBlock异步block

     //创建一个NSURLSession对象
        NSURLSession *session = [NSURLSession sharedSession];
        
        //创建一个URL对象
        NSURL *url = [NSURL URLWithString:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"];
        
        //创建一个请求
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
        [request setHTTPMethod:@"POST"];
        [request setHTTPBody:[@"date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213" dataUsingEncoding:NSUTF8StringEncoding]];
        
        NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
            //解析
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
            NSLog(@"%@",dic);
        }];
        [task resume];
  • 相关阅读:
    【LeetCode每天一题】Pascal's Triangle(杨辉三角)
    【Redis】持久化
    【LeetCode每天一题】Swap Nodes in Pairs
    【LeetCode每天一题】Reverse String
    [bzoj2152]聪聪可可
    [bzoj3572][Hnoi2014]世界树
    Codeforces Round#409/VK-Cup 2017 Round2
    Educational Codeforces Round#19
    [bzoj4813][Cqoi2017]小Q的棋盘
    [bzoj4236]JOIOJI
  • 原文地址:https://www.cnblogs.com/w150385/p/5251941.html
Copyright © 2020-2023  润新知