zoukankan      html  css  js  c++  java
  • NSURLSession的GET和POST下载

    
    

    * GET和POST在应用场合有什么区别呢?

    * GET方法向指定资源发出请求,发送的消息显示的跟在URL后面,用户信息不安全,并且传送信息量有限。(如下所示,在请求中能看到用户名和密码)

       http://localhost:8080/logandreg/logreg?name=wyg&pwd=1992

    * 如果仅仅是向服务器索要数据,没有参数,使用GET比较方便。(如下所示)

       http://www.baidu.com

    * POST传送的信息量大,并且传送的信息是被隐藏的,传送信息比较安全,如果向服务器传送数据,建议使用POST

     1 /*
     2      X-Code7NSURLConnection已经被弃用了,官方建议使用NSURLSession(ios7之后就已经出现了),在NSURLConnection的基础上进行的封装
     3      
     4      */
     5 #if 0
     6     NSString * path = @"http://10.0.8.8/sns/my/user_list.php?number=10&page=2";
     7     //如果有参数 在URL后面用?连接起来  参数 = 具体的数据
     8     //多个参数 用&连接起来
     9     //如果是直接在URL后面加上参数  这种事get请求  默认就是get请求
    10     NSURL * url = [NSURL URLWithString:path];
    11     
    12     //请求类对象
    13     NSURLRequest * request = [NSURLRequest requestWithURL:url];
    14     //NSURLSession对象
    15     NSURLSession * session = [NSURLSession sharedSession];
    16     //NSURLSession数据任务 网络连接
    17     NSURLSessionDataTask * task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    18        
    19         if (!error) {
    20             id obj = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    21             NSLog(@"%@",obj);
    22         }else{
    23             NSLog(@"%@",error);
    24         }
    25     }];
    26     //启动任务  启动下载
    27     [task resume];
    28 #else
    29     
    30     NSString * path = @"http://10.0.8.8/sns/my/user_list.php";
    31     
    32     NSURL * url = [NSURL URLWithString:path];
    33     
    34     NSMutableURLRequest * request = [[NSMutableURLRequest alloc]initWithURL:url];
    35     //如果不设置 默认就是get
    36     //设置请求的方式
    37     request.HTTPMethod = @"POST";
    38     //不能在URL后面直接拼它的参数
    39     
    40     //需要把参数写到请求的body里
    41     NSString * str = @"number=10&page=2";
    42     //设置请求的body
    43     request.HTTPBody = [str dataUsingEncoding:NSUTF8StringEncoding];
    44     
    45     NSURLSession * session = [NSURLSession sharedSession];
    46     
    47     NSURLSessionDataTask * task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    48         
    49         if (!error) {
    50             id obj = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    51             NSLog(@"%@",obj);
    52         }else{
    53             NSLog(@"%@",error);
    54         }
    55     }];
    56     
    57     [task resume];
    58     
    59 #endif
  • 相关阅读:
    IDEA 错误:程序包XXX不存在
    202A 202B 202C 202D 202E字符的作用及解释
    MySQL 获取每月多少日的sql写法
    Mybatis Plus使用租户过滤无效解决方案
    Shiro集成多个Realm,认证都不通过返回y configured realms. Please ensure that at least one realm can authenticate these tokens.
    使用IDEA开发SpringBoot不加载application.yml配置文件的解决方案
    集成SpringCloudBus,但没有总线通知更改
    Gradle 使用@Value注册编译报错
    Shiro Session放到Redis中常遇到的问题
    前端页面调试方式的选择
  • 原文地址:https://www.cnblogs.com/konglei/p/4830531.html
Copyright © 2011-2022 走看看