当前位置: 移动技术网 > IT编程>移动开发>IOS > 全面解析iOS中同步请求、异步请求、GET请求、POST请求

全面解析iOS中同步请求、异步请求、GET请求、POST请求

2019年07月24日  | 移动技术网IT编程  | 我要评论

银川论坛,赛尔号圣光萨米亚,超人钢铁之躯qvod

先给大家分别介绍下ios中同步请求、异步请求、get请求、post所代表的意思,然后在逐一通过实例给大家介绍。

1、同步请求可以从因特网请求数据,一旦发送同步请求,程序将停止用户交互,直至服务器返回数据完成,才可以进行下一步操作,

2、异步请求不会阻塞主线程,而会建立一个新的线程来操作,用户发出异步请求后,依然可以对ui进行操作,程序可以继续运行

3、get请求,将参数直接写在访问路径上。操作简单,不过容易被外界看到,安全性不高,地址最多255字节;

4、post请求,将参数放到body里面。post请求操作相对复杂,需要将参数和地址分开,不过安全性高,参数放在body里面,不易被捕获。

1、 同步get请求

//第一步,创建url
nsurl *url = [nsurl urlwithstring:@"http://api.hudong.com/iphonexml.do?type=focus-c"];
//第二步,通过url创建网络请求
nsurlrequest *request = [[nsurlrequest alloc]initwithurl:url cachepolicy:nsurlrequestuseprotocolcachepolicy timeoutinterval:10];
//nsurlrequest初始化方法第一个参数:请求访问路径,第二个参数:缓存协议,第三个参数:网络请求超时时间(秒)
其中缓存协议是个枚举类型包含:
nsurlrequestuseprotocolcachepolicy(基础策略)
nsurlrequestreloadignoringlocalcachedata(忽略本地缓存)
nsurlrequestreturncachedataelseload(首先使用缓存,如果没有本地缓存,才从原地址下载)
nsurlrequestreturncachedatadontload(使用本地缓存,从不下载,如果本地没有缓存,则请求失败,此策略多用于离线操作)
nsurlrequestreloadignoringlocalandremotecachedata(无视任何缓存策略,无论是本地的还是远程的,总是从原地址重新下载)
nsurlrequestreloadrevalidatingcachedata(如果本地缓存是有效的则不下载,其他任何情况都从原地址重新下载)
//第三步,连接服务器
nsdata *received = [nsurlconnection sendsynchronousrequest:request returningresponse:nil error:nil];
nsstring *str = [[nsstring alloc]initwithdata:received encoding:nsutf8stringencoding];
nslog(@"%@",str);

2、同步post请求

//第一步,创建url
nsurl *url = [nsurl urlwithstring:@"http://api.hudong.com/iphonexml.do"];
//第二步,创建请求
nsmutableurlrequest *request = [[nsmutableurlrequest alloc]initwithurl:url cachepolicy:nsurlrequestuseprotocolcachepolicy timeoutinterval:10];
[request sethttpmethod:@"post"];//设置请求方式为post,默认为get
nsstring *str = @"type=focus-c";//设置参数
nsdata *data = [str datausingencoding:nsutf8stringencoding];
[request sethttpbody:data];
//第三步,连接服务器
nsdata *received = [nsurlconnection sendsynchronousrequest:request returningresponse:nil error:nil];
nsstring *str1 = [[nsstring alloc]initwithdata:received encoding:nsutf8stringencoding];
nslog(@"%@",str1);

3、异步get请求

//第一步,创建url
nsurl *url = [nsurl urlwithstring:@"http://api.hudong.com/iphonexml.do?type=focus-c"];
//第二步,创建请求
nsurlrequest *request = [[nsurlrequest alloc]initwithurl:url cachepolicy:nsurlrequestuseprotocolcachepolicy timeoutinterval:10];
//第三步,连接服务器
nsurlconnection *connection = [[nsurlconnection alloc]initwithrequest:request delegate:self];

4、异步post请求

//第一步,创建url
nsurl *url = [nsurl urlwithstring:@"http://api.hudong.com/iphonexml.do"];
//第二步,创建请求
nsmutableurlrequest *request = [[nsmutableurlrequest alloc]initwithurl:url cachepolicy:nsurlrequestuseprotocolcachepolicy timeoutinterval:10];
[request sethttpmethod:@"post"];
nsstring *str = @"type=focus-c";
nsdata *data = [str datausingencoding:nsutf8stringencoding];
[request sethttpbody:data];
//第三步,连接服务器
nsurlconnection *connection = [[nsurlconnection alloc]initwithrequest:request delegate:self];

5、异步请求的代理方法

//接收到服务器回应的时候调用此方法
- (void)connection:(nsurlconnection *)connection didreceiveresponse:(nsurlresponse *)response
{
nshttpurlresponse *res = (nshttpurlresponse *)response;
nslog(@"%@",[res allheaderfields]);
self.receivedata = [nsmutabledata data];
}
//接收到服务器传输数据的时候调用,此方法根据数据大小执行若干次
-(void)connection:(nsurlconnection *)connection didreceivedata:(nsdata *)data
{
[self.receivedata appenddata:data];
}
//数据传完之后调用此方法
-(void)connectiondidfinishloading:(nsurlconnection *)connection
{
nsstring *receivestr = [[nsstring alloc]initwithdata:self.receivedata encoding:nsutf8stringencoding];
nslog(@"%@",receivestr);
}
//网络请求过程中,出现任何错误(断网,连接超时等)会进入此方法
-(void)connection:(nsurlconnection *)connection
didfailwitherror:(nserror *)error
{
nslog(@"%@",[error localizeddescription]);

以上所述是小编给大家介绍的ios中同步请求、异步请求、get请求、post请求全面解析,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

  • ios uicollectionview实现横向滚动

    现在使用卡片效果的app很多,之前公司让实现一种卡片效果,就写了一篇关于实现卡片的文章。文章最后附有demo实现上我选择了使用uicollectionview ... [阅读全文]
  • iOS UICollectionView实现横向滑动

    本文实例为大家分享了ios uicollectionview实现横向滑动的具体代码,供大家参考,具体内容如下uicollectionview的横向滚动,目前我使... [阅读全文]
  • iOS13适配深色模式(Dark Mode)的实现

    iOS13适配深色模式(Dark Mode)的实现

    好像大概也许是一年前, mac os系统发布了深色模式外观, 看着挺刺激, 时至今日用着也还挺爽的终于, 随着iphone11等新手机的发售, ios 13系统... [阅读全文]
  • ios 使用xcode11 新建项目工程的步骤详解

    ios 使用xcode11 新建项目工程的步骤详解

    xcode11新建项目工程,新增了scenedelegate这个类,转而将原appdelegate负责的对ui生命周期的处理担子接了过来。故此可以理解为:ios... [阅读全文]
  • iOS实现转盘效果

    本文实例为大家分享了ios实现转盘效果的具体代码,供大家参考,具体内容如下demo下载地址: ios转盘效果功能:实现了常用的ios转盘效果,轮盘抽奖效果的实现... [阅读全文]
  • iOS开发实现转盘功能

    本文实例为大家分享了ios实现转盘功能的具体代码,供大家参考,具体内容如下今天给同学们讲解一下一个转盘选号的功能,直接上代码直接看viewcontroller#... [阅读全文]
  • iOS实现轮盘动态效果

    本文实例为大家分享了ios实现轮盘动态效果的具体代码,供大家参考,具体内容如下一个常用的绘图,主要用来打分之类的动画,效果如下。主要是ios的绘图和动画,本来想... [阅读全文]
  • iOS实现九宫格连线手势解锁

    本文实例为大家分享了ios实现九宫格连线手势解锁的具体代码,供大家参考,具体内容如下demo下载地址:效果图:核心代码://// clockview.m// 手... [阅读全文]
  • iOS实现卡片堆叠效果

    本文实例为大家分享了ios实现卡片堆叠效果的具体代码,供大家参考,具体内容如下如图,这就是最终效果。去年安卓5.0发布的时候,当我看到安卓全新的material... [阅读全文]
  • iOS利用余弦函数实现卡片浏览工具

    iOS利用余弦函数实现卡片浏览工具

    本文实例为大家分享了ios利用余弦函数实现卡片浏览工具的具体代码,供大家参考,具体内容如下一、实现效果通过拖拽屏幕实现卡片移动,左右两侧的卡片随着拖动变小,中间... [阅读全文]
验证码:
移动技术网