zoukankan      html  css  js  c++  java
  • Get 和 Post方法的登录

     1. Get & Post

     1> Get请求直接从服务器拿数据

        性能好

        效率高

        在地址栏会显示所有的参数,从直观上安全性不高

        由于Get不提交数据给服务器,因此实际的安全性高

        实际应用:数据查询

     2> Post请求,需要先弄一个数据体,将数据体提交给服务器,才能获取到服务器的响应

        性能不好

        效率低

        不会在地址栏显示参数,直观上安全性高

        由于Post请求会提交数据给服务器,有可能会存在安全漏洞,实际的安全性不高

        实际应用:用户登录、上传文件等需要与服务器进行数据交互的操作,才需要使用到Post操作

    1.Get方法

     1 #pragma mark 返回Get登录请求
     2 - (NSURLRequest *)getLoginRequest
     3 {
     4     NSString *userName = _userName.text;
     5     NSString *password = [_password.text companyMD5];
     6     NSLog(@"%@", [_password.text MD5]);
     7     NSLog(@"%@", password);
     8     
     9     // 1. 网络地址URL
    10     NSString *urlString = [NSString stringWithFormat:@"http://192.168.3.251/~apple/itcast/login.php?username=%@&password=%@", userName, password       ];
    11     urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    12     
    13     NSURL *url = [NSURL URLWithString:urlString];
    14     
    15     // 2. 请求
    16     // 1> url
    17     // 2> 缓存策略
    18     // 3> 超时时长
    19     // 提示:因为网络的状态是未知的,因此要使用URLRequest一定要指定超时时长
    20     // 否则会严重影响用户体验!
    21     return [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:2.0f];
    22    
    23 }
    24 
    25
    26 #pragma mark Get方法登录
    27 - (IBAction)getLogin
    28 {
    29     // 1. 建立请求
    30     NSURLRequest *request = [self getLoginRequest];
    31     
    32     // 3. 连接
    33     NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
    34     
    35     // 4. 启动连接
    36     [connection start];
    37     
    38     // 5. 实例化接收数据
    39     _serverData = [NSMutableData data];
    40 }

    2.Post方法

     1 - (IBAction)postLogin
     2 {
     3     NSString *userName = _userName.text;
     4     NSString *password = _password.text;
     5     
     6     // 1. 网络地址URL
     7     NSString *urlString = [NSString stringWithFormat:@"http://192.168.3.251/~apple/itcast/login.php"];
     8     
     9     NSURL *url = [NSURL URLWithString:urlString];
    10     
    11     // 2. 请求,生成数据体添加到请求
    12     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    13     
    14     // 1) 指定网络请求的方法
    15     // 默认是GET,POST请求通常用在用户登录,上传文件
    16     request.HTTPMethod = @"POST";
    17     
    18     // 2) 生成数据体
    19     NSString *bodyStr = [NSString stringWithFormat:@"username=%@&password=%@", userName, password];
    20     // 转换成NSData
    21     request.HTTPBody = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
    22     
    23     // 3. 连接
    24     NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
    25     
    26     // 4. 启动连接
    27     [connection start];
    28     
    29     // 5. 实例化接收数据
    30     _serverData = [NSMutableData data];
    31 }
  • 相关阅读:
    cygwin下安装scws
    atitit.提升软件开发的生产力关健点-------大型开发工具最关健
    atitit。mssql sql server 转换mysql 及 分页sql ast的搭建
    atitit.手动配置列表文件的选择and 数据的层次结构 attilax总结最佳实践--yaml
    atitit.获取connection hibernate4
    atitit.新增编辑功能 跟orm的实现 attilax p31
    atitit.自动生成数据库结构脚本,或者更换数据库,基于hibernate4
    atitit.Windows Server 2003 2008 2012系统的新特性 attilax 总结
    atitit.orm的缺点与orm框架市场占有率,选型attilax总结
    atitit。解决 No suitable Log constructor。。NoClassDefFoundError: org/apache/log4j/Category 找不到类的
  • 原文地址:https://www.cnblogs.com/hkyangvip/p/3496633.html
Copyright © 2011-2022 走看看