没有合适的资源?快使用搜索试试~ 我知道了~
首页ASIHttpRequest库API的介绍和使用
ASIHttpRequest库API的介绍和使用
![](https://csdnimg.cn/release/wenkucmsfe/public/img/star.98a08eaa.png)
ASIHttpRequest库是iphone下一个http网络访问的第三方库,该库主要是用core sevice开发的。这本文档完整的介绍了ASIHttpRequest库的用法,对于想使用该库开发的人,这是一个不可多得的资料。
资源详情
资源推荐
![](https://csdnimg.cn/release/download_crawler_static/3871257/bg1.jpg)
一 ASIHttpRequest 库简介、配置和安装
发布者:
Seven's - 2011/10/12 -
分类:
ASIHTTPRequest
中文文档
使用 ASIHTTPRequest 可以很方便的进行一下操作:
• 同步/异步方式下载数据
• 定义下载队列,让队列中的任务按指定的并发数来下载(队列下载必须是异步的)
• 提交表单,文件上传
• 处理 cookie
• 设置代理
• 上下载进度条
• 重定向处理
• 请求与响应的 GZIP
• 验证与授权
等等,只要跟 HTTP 有关,只有你想不到的,没有她做不到的~
配置方法:
• ASIHTTPRequestConfig.h
• ASIHTTPRequestDelegate.h
• ASIProgressDelegate.h
• ASICacheDelegate.h
• ASIHTTPRequest.h
• ASIHTTPRequest.m
• ASIDataCompressor.h
• ASIDataCompressor.m
• ASIDataDecompressor.h
• ASIDataDecompressor.m
• ASIFormDataRequest.h
• ASIInputStream.h
• ASIInputStream.m
• ASIFormDataRequest.m
• ASINetworkQueue.h
• ASINetworkQueue.m
• ASIDownloadCache.h
• ASIDownloadCache.m
iPhone 工程还需要:
• ASIAuthenticationDialog.h
• ASIAuthenticationDialog.m
• Reachability.h (在 External/Reachability 目录下)
• Reachability.m (在 External/Reachability 目录下)
库引用:
CFNetwork.framework、
SystemConfiguration.framework,MobileCoreServices.framework,CoreGraphics.framewo
rk 和 libz.dylib
另外,还需要 libxml2.dylib(libxml2 还需要设置连接选项-lxml2 和头文件搜索路径
/usr/include/libxml2)
![](https://csdnimg.cn/release/download_crawler_static/3871257/bg2.jpg)
二 ASIHttpRequest-创建和执行 request
发布者:
Seven's - 2011/10/12 -
分类:
ASIHTTPRequest
中文文档
同步请求
同步请求会在当前线程中执行,使用 error 属性来检查结束状态(要下载大文件,则需
要设定 downloadDestinationPath 来保存文件到本地):
1""
2""
3""
4""
5""
6""
7""
8""
9""
10"
- (IBAction)grabURL:(id)sender""
{""
NSURL"*url = [NSURL"URLWithString:@"http://www.dreamingwish.com"];""
ASIHTTPRequest *request = [ASIHTTPRequest"requestWithURL:url];""
[request"startSynchronous];""
NSError"*error = [request"error];""
if"(!error) {""
NSString"*response = [request"responseString];""
}""
}"
同步请求会阻塞主线程的执行,这导致用户界面不响应用户操作,任何动画都会停止
渲染。
异步请求
下面是最简单的异步请求方法,这个 request 会在全局的 NSOperationQueue 中执行,
若要进行更复杂的操作,我们需要自己创建 NSOperationQueue 或者 ASINetworkQueue,
后面会讲到。
1""
2""
3""
4""
5""
6""
7""
8""
9""
10""
11""
12""
13""
14""
15""
16""
17""
18""
19""
20""
21"
- (IBAction)grabURLInBackground:(id)sender""
{""
NSURL"*url = [NSURL"URLWithString:@"http://www.dreamingwish.com"];""
ASIHTTPRequest *request = [ASIHTTPRequest"requestWithURL:url];""
[request"setDelegate:self];""
[request"startAsynchronous];""
}""
"""
- (void)requestFinished:(ASIHTTPRequest *)request""
{""
// Use when fetching text data""
NSString"*responseString = [request"responseString];""
"""
// Use when fetching binary data""
NSData"*responseData = [request"responseData];""
}""
"""
- (void)requestFailed:(ASIHTTPRequest *)request""
{""
NSError"*error = [request"error];""
}"
![](https://csdnimg.cn/release/download_crawler_static/3871257/bg3.jpg)
!
使用 block!
block 这个东西,太鸡肋了。。。当然特定时候还是需要的。
在平台支持情况下,ASIHTTPRequest1.8 以上支持 block。
1""
2""
3""
4""
5""
6""
7""
8""
9""
10""
11""
12""
13""
14""
15""
16"
- (IBAction)grabURLInBackground:(id)sender""
{""
NSURL"*url = [NSURL"URLWithString:@"http://allseeing-i.com"];""
__block ASIHTTPRequest *request = [ASIHTTPRequest"requestWithURL:url];""
[request"setCompletionBlock:^{""
// Use when fetching text data""
NSString"*responseString = [request"responseString];""
"""
// Use when fetching binary data""
NSData"*responseData = [request"responseData];""
}];""
[request"setFailedBlock:^{""
NSError"*error = [request"error];" "
}];""
[request"startAsynchronous];""
}"
注意,声明 request 时要使用__block 修饰符,这是为了告诉 block 不要 retain
request,以免出现 retain 循环,因为 request 是会 retain block 的。
使用队列
创建 NSOperationQueue 或者 ASINetworkQueue 队列,我们还可以设定最大并发连
接数:maxConcurrentOperationCount
1""
2""
3""
4""
5""
6""
7""
8""
9""
10""
11""
12""
13""
14""
15""
16""
17""
18""
- (IBAction)grabURLInTheBackground:(id)sender""
{""
if"(![self"queue]) {""
[self"setQueue:[[[NSOperationQueue"alloc]"init]"autorelease]];""
[self"queue].maxConcurrentOperationCount ="4;""
}""
"""
NSURL"*url = [NSURL"URLWithString:@"http://www.dreamingwish.com"];""
ASIHTTPRequest *request = [ASIHTTPRequest"requestWithURL:url];""
[request"setDelegate:self];""
[request"setDidFinishSelector:@selector(requestDone:)];""
[request"setDidFailSelector:@selector(requestWentWrong:)];""
[[self"queue]"addOperation:request]; //queue is an NSOperationQueue""
}""
"""
- (void)requestDone:(ASIHTTPRequest *)request""
{""
NSString"*response = [request"responseString];""
}""
"""
- (void)requestWentWrong:(ASIHTTPRequest *)request""
{""
![](https://csdnimg.cn/release/download_crawler_static/3871257/bg4.jpg)
19""
20""
21""
22""
23""
24"
NSError"*error = [request"error];""
}"
如果不设定 selector,那么系统会使用默认的 requestFinished: 和 requestFailed:方
法
如果需要对队列里面的每个 request 进行区分,那么可以设定 request 的 userInfo 属
性,它是个 NSDictionary,或者更简单的方法是设定每个 request 的 tag 属性,这两个属
性都不会被发送到服务器。
不要使用
request
的
URL
来区分每个
request
,因为
URL
可能会改变(例如重定向),
如果需要使用
request
的
URL
,使用
[request originalURL]
,这个将永远返回第一个
url
。
对于 ASINetworkQueue
ASINetworkQueue 是 NSOperationQueue 的子类,提供更高级的特性
(ASINetworkQueue 的代理函数):
• requestDidStartSelector
当一个 request 开始执行时,这个代理函数会被调用。
• requestDidReceiveResponseHeadersSelector
当队列中的 request 收到服务器返回的头信息时,这个代理函数会被调用。对于下
载很大的文件,这个通常比整个 request 的完成要早。
• requestDidFinishSelector
当每个 request 完成时,这个代理函数会被调用。
• requestDidFailSelector
当每个 request 失败时,这个代理函数会被调用。
• queueDidFinishSelector
当队列完成(无论 request 失败还是成功)时,这个代理函数会被调用。
ASINetworkQueues 与 NSOperationQueues 稍有不同,加入队列的 request 不会立
即开始执行。如果队列打开了进度开关,那么队列开始时,会先对所有 GET 型 request 进
行一次 HEAD 请求,获得总下载大小,然后真正的 request 才被执行。
向一个已经开始进行的
ASINetworkQueue
加入
request
会怎样?
如果你使用
ASINetworkQueue
来跟踪若干
request
的进度,只有当新的
request
开始
执行时,总进度才会进行自适应调整(向后移动)。
ASINetworkQueue
不会为队列开始后
才加入的
request
进行
HEAD
请求,所以如果你一次向一个正在执行的队列加入很多
request
,那么总进度不会立即被更新。
如果队列已经开始了,不需要再次调用
[queue go]
。
当 ASINetworkQueue 中的一个 request 失败时,默认情况下,ASINetworkQueue 会
取消所有其他的 request。要禁用这个特性,设置 [queue
setShouldCancelAllRequestsOnFailure:NO]。
ASINetworkQueues 只可以执行 ASIHTTPRequest 操作,二不可以用于通用操作。试
图加入一个不是 ASIHTTPRequest 的 NSOperation 将会导致抛出错误。
![](https://csdnimg.cn/release/download_crawler_static/3871257/bg5.jpg)
!
取消异步请求!
取消一个异步请求(无论 request 是由[request startAsynchronous]开始的还是从你创
建的队列中开始的),使用[request cancel]即可。注意同步请求不可以被取消。
注意,如果你取消了一个 request,那么这个 request 将会被视为请求失败,并且
request 的代理或者队列的代理的失败代理函数将被调用。如果你不想让代理函数被调用,
那么将 delegate 设置为 nil,或者使用 clearDelegatesAndCancel 方法来取消 request。
clearDelegatesAndCancel 将会首先清除所有的代理和 block。
当使用 ASINetworkQueue 时,如果取消了队列中的一个 request,那么队列中其他所
有 request 都会被取消,可以设置 shouldCancelAllRequestsOnFailure 的值为 NO 来避
免这个现象。
!
安全地控制 delegate 防止 request 完成之前代理被释放!
request 并不 retain 它们的代理,所以有可能你已经释放了代理,而之后 request 完成
了,这将会引起崩溃。大多数情况下,如果你的代理即将被释放,你一定也希望取消所有
request,因为你已经不再关心它们的返回情况了。如此做:
1""
2""
3""
4""
5""
6""
7""
8"
// 代理类的 dealloc 函数""
- (void)dealloc""
{""
[request"clearDelegatesAndCancel];""
[request"release];""
...""
[super"dealloc];""
}"
剩余26页未读,继续阅读
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![DOC](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![sb3](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://profile-avatar.csdnimg.cn/default.jpg!1)
animeng
- 粉丝: 196
- 资源: 14
上传资源 快速赚钱
我的内容管理 收起
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![](https://csdnimg.cn/release/wenkucmsfe/public/img/voice.245cc511.png)
会员权益专享
最新资源
- 共轴极紫外投影光刻物镜设计研究
- 基于GIS的通信管线管理系统构建与音视频编解码技术应用
- 单站被动目标跟踪算法:空频域信息下的深度研究与进展
- 构建通信企业工程项目的项目管理成熟度模型:理论与应用
- 基于控制理论的主动队列管理算法与稳定性分析
- 谷歌文件系统下的实用网络编码技术在分布式存储中的应用
- CMOS图像传感器快门特性与运动物体测量研究
- 深孔采矿研究:3D数据库在采场损失与稳定性控制中的应用
- 《洛神赋图》图像研究:明清以来的艺术价值与历史意义
- 故宫藏《洛神赋图》图像研究:明清艺术价值与审美的飞跃
- 分布式视频编码:无反馈通道算法与复杂运动场景优化
- 混沌信号的研究:产生、处理与通信系统应用
- 基于累加器的DSP数据通路内建自测试技术研究
- 跨国媒体对南亚农村社会的影响:以斯里兰卡案例的社会学分析
- 散单元法与CFD结合模拟气力输送研究
- 基于粒化机理的粗糙特征选择算法:海量数据高效处理研究
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![](https://img-home.csdnimg.cn/images/20220527035711.png)
![](https://img-home.csdnimg.cn/images/20220527035711.png)
![](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![](https://csdnimg.cn/release/wenkucmsfe/public/img/green-success.6a4acb44.png)