当前位置: 移动技术网 > IT编程>移动开发>IOS > iOS的HTTP请求和请求回执类用法小结

iOS的HTTP请求和请求回执类用法小结

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

请求类nsurlrequest
nsurlrequest类中常用方法和属性总结:

//通过类方法创建默认的请求对象
/*
通过这种方式创建的请求对象 默认使用nsurlrequestuseprotocolcachepolicy缓存逻辑 默认请求超时时限为60s
*/
+ (instancetype)requestwithurl:(nsurl *)url;
//返回一个bool值 用于判断是否支持安全编码
+ (bool)supportssecurecoding;
//请求对象的初始化方法 创建时设置缓存逻辑和超时时限
+ (instancetype)requestwithurl:(nsurl *)url cachepolicy:(nsurlrequestcachepolicy)cachepolicy timeoutinterval:(nstimeinterval)timeoutinterval;
//init方法进行对象的创建 默认使用nsurlrequestuseprotocolcachepolicy缓存逻辑 默认请求超时时限为60s
- (instancetype)initwithurl:(nsurl *)url;
//init方法进行对象的创建
- (instancetype)initwithurl:(nsurl *)url cachepolicy:(nsurlrequestcachepolicy)cachepolicy timeoutinterval:(nstimeinterval)timeoutinterval;
//只读属性 获取请求对象的url
@property (nullable, readonly, copy) nsurl *url;
//只读属性 缓存策略枚举
/*
nsurlrequestcachepolicy枚举如下:
typedef ns_enum(nsuinteger, nsurlrequestcachepolicy)
{
    //默认的缓存协议
    nsurlrequestuseprotocolcachepolicy = 0,
    //无论有无本地缓存数据 都进行从新请求
    nsurlrequestreloadignoringlocalcachedata = 1,
    //忽略本地和远程的缓存数据 未实现的策略
    nsurlrequestreloadignoringlocalandremotecachedata = 4,
    //无论有无缓存数据 都进行从新请求
    nsurlrequestreloadignoringcachedata = nsurlrequestreloadignoringlocalcachedata,
    //先检查缓存 如果没有缓存再进行请求
    nsurlrequestreturncachedataelseload = 2,
    //类似离线模式,只读缓存 无论有无缓存都不进行请求
    nsurlrequestreturncachedatadontload = 3,
    //未实现的策略
    nsurlrequestreloadrevalidatingcachedata = 5, // unimplemented
};
*/
@property (readonly) nsurlrequestcachepolicy cachepolicy;
//只读属性 获取请求的超时时限
@property (readonly) nstimeinterval timeoutinterval;
//主文档地址 这个地址用来存放缓存
@property (nullable, readonly, copy) nsurl *maindocumenturl;
//获取网络请求的服务类型 枚举如下
/*
typedef ns_enum(nsuinteger, nsurlrequestnetworkservicetype)
{
    nsurlnetworkservicetypedefault = 0, // standard internet traffic
    nsurlnetworkservicetypevoip = 1, // voice over ip control traffic
    nsurlnetworkservicetypevideo = 2, // video traffic
    nsurlnetworkservicetypebackground = 3, // background traffic
    nsurlnetworkservicetypevoice = 4    // voice data
};
*/
@property (readonly) nsurlrequestnetworkservicetype networkservicetype;
//获取是否允许使用服务商蜂窝网络
@property (readonly) bool allowscellularaccess;
nsurlrequest请求类除了在初始化时可以设定一些属性,创建出来后则大部分属性都为只读的,无法设置与修改。另一个类nsmutableurlrequest可以更加灵活的设置请求的相关属性。

nsmutableurlrequest类中常用方法与属性总结

//设置请求的url
@property (nullable, copy) nsurl *url;
//设置请求的缓存策略
@property nsurlrequestcachepolicy cachepolicy;
//设置超时时间
@property nstimeinterval timeoutinterval;
//设置缓存目录
@property (nullable, copy) nsurl *maindocumenturl;
//设置网络服务类型
@property nsurlrequestnetworkservicetype networkservicetype ns_available(10_7, 4_0);
//设置是否允许使用服务商蜂窝网
@property bool allowscellularaccess ns_available(10_8, 6_0);

nsurlrequest请求对象与http/https协议相关请求的属性设置:

以下属性的设置必须使用nsmutableurlrequest类,如果是nsurlrequest,则只可以读,不可以修改。

//设置hppt请求方式 默认为“get”
@property (copy) nsstring *httpmethod;
//通过字典设置http请求头的键值数据
@property (nullable, copy) nsdictionary<nsstring *, nsstring *> *allhttpheaderfields;
//设置http请求头中的字段值
- (void)setvalue:(nullable nsstring *)value forhttpheaderfield:(nsstring *)field;
//向http请求头中添加一个字段
- (void)addvalue:(nsstring *)value forhttpheaderfield:(nsstring *)field;
//设置http请求体 用于post请求
@property (nullable, copy) nsdata *httpbody;
//设置http请求体的输入流
@property (nullable, retain) nsinputstream *httpbodystream;
//设置发送请求时是否发送cookie数据
@property bool httpshouldhandlecookies;
//设置请求时是否按顺序收发 默认禁用 在某些服务器中设为yes可以提高网络性能
@property bool httpshouldusepipelining;

请求回执类nsurlresponse属性简介
nsurlresponse类中存放请求的回执信息,在发送网络请求时,如果请求成功,首先会接收到服务端的回执信息,直接开始接收具体的返回数据。nsurlresponse对象中主要有以下属性:

//请求的url地址
@property (nullable, readonly, copy) nsurl *url;
//返回数据的数据类型
@property (nullable, readonly, copy) nsstring *mimetype;
//获取返回数据的内容长度
@property (readonly) long long expectedcontentlength;
//获取返回数据的编码方式
@property (nullable, readonly, copy) nsstring *textencodingname;
//返回拼接的数据文件名 以url为名 数据没醒mimetype为扩展名
@property (nullable, readonly, copy) nsstring *suggestedfilename;
对于http请求,请求回执会被封装为nshttpurlresponse对象,其中除了有上面那些属性外,还有如下的扩展属性:

//请求的状态码
@property (readonly) nsinteger statuscode;
//请求头中所有的字段
@property (readonly, copy) nsdictionary *allheaderfields;

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

相关文章:

  • 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利用余弦函数实现卡片浏览工具的具体代码,供大家参考,具体内容如下一、实现效果通过拖拽屏幕实现卡片移动,左右两侧的卡片随着拖动变小,中间... [阅读全文]
验证码:
移动技术网