博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS网络基础
阅读量:4501 次
发布时间:2019-06-08

本文共 3651 字,大约阅读时间需要 12 分钟。

Demo

#import "SDViewController.h"@interface SDViewController ()@property (weak, nonatomic) IBOutlet UITextField *userName;@property (weak, nonatomic) IBOutlet UITextField *password;@end@implementation SDViewController/** *  网络请求步骤 *  1、URL 确定资源 *  NSString *urlStrl = @""; *  NSURL *url = [NSURL URLWithString:urlStrl]; *   *  (1)GET   URL中包含参数 *  (2)POST  URL中包含参数 *   *  2、建立请求 URLRequest *  (1)    GET 不需要对请求参数做处理 *          URLRequest * *  (2)    POST 需要在请求中包装参数, 指定HTTP方法和HTTP数据体 *       NSMutableURLRequest *      HTTPMethod = @"POST" *       HTPPBody = 包含登录信息的二进制数据 *  3、发送请求 *       !!!在实际开发中,所有的网络请求都是异步的 *      NSURLConnection sendAsynchronousRequest *       在请求的异步方法中,对接收到的数据进行处理! *///登陆按钮点击事件- (IBAction)loginBtnClick:(id)sender{//    [self getLoginWithUserName:self.userName.text andPassword:self.password.text];    [self postLoninWithUserName:self.userName.text andPassword:self.password.text];}/** GET请求登陆*/- (void)getLoginWithUserName:(NSString *)userName andPassword:(NSString *)password{    //1、url -- 准备资源    NSString *urlStr = [NSString stringWithFormat:@"http://localhost/login.php?username=%@&password=%@",userName,password];    NSURL *url = [NSURL URLWithString:urlStr];        //2、创建网络请求    //URLRequest请求默认是GET    NSURLRequest *request = [NSURLRequest requestWithURL:url];        //3、发送网络请求    //所有的网络请求都是使用异步方法    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {        // 1> 将返回的二进制数据,转换成字符串        // (1) 将二进制数据转换成字符串没有直接的类方法,需要alloc initWithData        // (2) 提示:在开发网络时,如果需要跟踪网络返回的具体内容,经常会先将data转换成字符串输出!        // 转换成字符串的方式,在调试中经常使用!        NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];        NSLog(@"result -- %@",result);                NSString *respond = [NSString stringWithFormat:@"respond -- %@",response];        NSLog(@"respond -- %@",respond);                //JSON转换,格式是和NSDictionary的快速包装方式非常像        //将JSON转换成字典        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:NULL];                NSLog(@"dict -- %@",dict);    }];    }/** POST请求登陆*/- (void)postLoninWithUserName:(NSString *)userName andPassword:(NSString *)password{    //1、url  准备资源    NSString *urlStr = @"http://localhost/login.php";    NSURL *url = [NSURL URLWithString:urlStr];        //2、request,POST方法,需要建立一个可变的请求    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];    request.HTTPMethod = @"POST";  //设置请求方式        //2.1数据体    NSString *bodyStr = [NSString stringWithFormat:@"username=%@&password=%@",userName,password];    //将字符串转换成二进制数    request.HTTPBody = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];        //3、发送请求    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {        // 1> 将返回的二进制数据,转换成字符串        // (1) 将二进制数据转换成字符串没有直接的类方法,需要alloc initWithData        // (2) 提示:在开发网络时,如果需要跟踪网络返回的具体内容,经常会先将data转换成字符串输出!        // 转换成字符串的方式,在调试中经常使用!        NSLog(@"response - %@",response);                NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];        NSLog(@"result -- %@",result);                NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:NULL];        NSLog(@"dict -- %@",dict);                NSError *erro  = connectionError;        NSLog(@"erro -- %@",erro);                 }];    }@end

  

转载于:https://www.cnblogs.com/ndyBlog/p/3971291.html

你可能感兴趣的文章
Cocos2d-x 3.0 编译出错 解决 error: expected ';' at end of member declaration
查看>>
Ubuntu12.04下载Repo
查看>>
python基础教程_学习笔记10:异常
查看>>
MATLAB——scatter的简单应用
查看>>
linux下复制粘贴快捷键
查看>>
什么是对象
查看>>
记录开发小程序
查看>>
WinSock服务程序
查看>>
巴西柔术第五课:过腿
查看>>
文件的操作
查看>>
网上图书商城项目学习笔记-007登录功能实现
查看>>
关于mysql的级联删除(之前好多人咨询过我)
查看>>
Linux环境下的C/C+基础调试技术2——程序控制
查看>>
wpf动画同步闪烁
查看>>
3.16上午 复习雅思核心词+新单词100个
查看>>
Html5 部分特性
查看>>
前端工具集合记录
查看>>
浅析负载均衡的6种算法,Ngnix的5种算法
查看>>
OpenCV——图像修补
查看>>
网络概述
查看>>