ios中的 Get & Post

  1. Get & Post

1> Get请求直接从服务器拿数据 性能好 效率高

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

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

实际应用:数据查询

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

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

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

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

下面是我post和get方法的关键代码 主要是一个用户名和密码的登录界面 界面的代码我就省略了,我只提供关键代码,如果有什么疑问,可以联系本人,共同探讨:email:624204727@qq.com

<!-- lang: cpp -->
#pragma mark Get方法登录
  • (IBAction)getLogin { NSString *userName = _userName.text; NSString *password = _password.text;

    // 1. 网络地址URL NSString *urlString = [NSString stringWithFormat:@"http://192.168.3.251/~apple/itcast/login.php?username=%@&password=%@", userName, password]; urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSURL *url = [NSURL URLWithString:urlString];

    // 2. 请求 NSURLRequest *request = [NSURLRequest requestWithURL:url];

    // 3. 连接 NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];

    // 4. 启动连接 [connection start];

    // 5. 实例化接收数据 _serverData = [NSMutableData data]; }

#pragma mark POST登录

  • (IBAction)postLogin { NSString *userName = _userName.text; NSString *password = _password.text;

    // 1. 网络地址URL NSString *urlString = [NSString stringWithFormat:@"http://192.168.3.251/~apple/itcast/login.php"];

    NSURL *url = [NSURL URLWithString:urlString];

    // 2. 请求,生成数据体添加到请求 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    // 1) 指定网络请求的方法 // 默认是GET,POST请求通常用在用户登录,上传文件 request.HTTPMethod = @"POST";

    // 2) 生成数据体 NSString *bodyStr = [NSString stringWithFormat:@"username=%@&password=%@", userName, password]; // 转换成NSData request.HTTPBody = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];

    // 3. 连接 NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];

    // 4. 启动连接 [connection start];

    // 5. 实例化接收数据 _serverData = [NSMutableData data]; }

#pragma mark - 网络请求代理方法 #pragma mark 开始接收数据,拼接数据

  • (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [_serverData appendData:data]; }

#pragma mark 数据接收完成处理

  • (void)connectionDidFinishLoading:(NSURLConnection *)connection { // 转换成字符串 NSString *result = [[NSString alloc] initWithData:_serverData encoding:NSUTF8StringEncoding];

    NSLog(@"%@", result); [self showMessageWithResult:result];

    // 释放数据 _serverData = nil; }

#pragma mark 错误处理

  • (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"%@", error.localizedDescription); }

#pragma mark - 私有方法 #pragma mark 处理服务器返回数据

  • (void)showMessageWithResult:(NSString *)result { // 判断“用户名”所在位置 NSRange range = [result rangeOfString:@"用户名"]; NSLog(@"%@", NSStringFromRange(range));

    // range.location > 0 说明用户登录正确 NSString *message = nil;

    if (range.location > 0) { NSString *userName = [result substringFromIndex:range.location + range.length]; NSLog(@"%@", userName);

      message = [NSString stringWithFormat:@"欢迎归来:%@", userName];
    

    } else { // 在网络登录时,千万不要告诉用户什么错了! message = @"用户名或者密码错误!"; }

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

    [alert show]; }

转载于:https://my.oschina.net/panyong/blog/196972

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值