ASIHTTPRequest提供以下功能:
一、提交数据到web服务器或者从web服务器得到数据;
二、下载数据到内存或磁盘;
三、采用html input相同的机制上传文件;
四、断点续传;
五、简单存取HTTP头;
六、上传/下载进度显示;
七、支持Cookie;
八、后台运行(iOS4.0以上支持);
九、对于请求和响应的GZIP支持;
十、支持客户端证书;
十一、支持同步/异步请求;
...... html
关于它的介绍网上已经有不少了,该项目有很详细的指南文档:How to use ASIHTTPRequest,也有网友翻译成中文了。本文没有彻底照搬官方文档的内容,而是着重介绍了几个常见的应用,并涵盖了一些本身的理解和实际应用经验,包括:
安装、简单异步/同步请求、队列请求、上传、下载及 Cookies
注意,虽然这些技术在本文中是分开讲述的,但在实际工做中,每每是多种技术结合应用的。
此外,Http请求每每伴随着XML技术的应用,实际上ASIHTTPRequest是能够和SAX异步解析结合应用的,这部份内容请参考《ASIHTTPRequest和libxml结合,实现边请求边解析》。 git
项目下载地址: http://github.com/pokeb/asi-http-request/tarball/master
解压缩并打开文件夹,其中包括:
一、一个iPhone Xcode项目(源文件)
二、一个Mac Xcode项目(源文件)
三、一个iPhone下使用的Sample Code(源文件)
四、一个Mac下使用的Sample Code(源文件)
五、一个Readme.texttile,关于该项目的介绍
其实全部的内容都在其中了,若是你是初学者,不知到怎么下手,能够看 http://allseeing-i.com/ASIHTTPRequest/How-to-use这里有一份详细的入门指南。
如今,咱们要作的就是,在本身的项目中使用它。 github
1、在项目中使用ASIHTTPRequest
一、拷贝源文件到项目中
ASIHTTPRequest是一个开源项目,要使用它,直接拷贝项目源文件到你的项目中,包括下列文件(即Classes下全部文件和External/Reachability下全部文件):
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目录) web
二、添加依赖库 服务器
ASIHTTPRequest依赖于如下5个框架或库:
CFNetwork、SystemConfiguration、MobileCoreServices、CoreGraphics和libz1.2.3。
依次将上述库和框架添加到target的Linked Libraries中。
框架
2、简单的同步请求示例
新建iOS项目,加入必需的源文件和Linked Libraries。往MainWindow.xib中添加一个UIButton,添加相应的outlet与action。编写按钮的Touch up inside代码,并链接到UIButton:
异步
- (IBAction)goURL { NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"]; // 构造ASIHTTPRequest对象 ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; // 开始同步请求 [request startSynchronous]; NSError *erro = [request error]; assert(!erro); // 若是请求成功,返回Response NSString *response = [request responseString]; NSLog(@"%@", response); }
别忘了在适当的地方导入ASIHTTPRequest:#import "ASIHTTPRequest.h"分别保存IB和Xcode中所作的更改,⌘+B编译。 ide
3、简单的异步请求示例
将上述代码修改成:
学习
- (IBAction)goURL { NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; // 设定委托,委托本身实现异步请求方法 [request setDelegate: self]; // 开始异步请求 [request startAsynchronous]; }
并 实现一系列委托方法:(别忘了在.h中添加import头文件以及delegate)
this
- (IBAction)goURL { NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"]; __block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; // ASIHTTPRequest支持iOS4.0的块语法,能够把委托方法定义到块中 [request setCompletionBlock:^{ // 请求响应结束,返回response NSString *response = [request responseString]; // 对于2进制数据,使用:NSData *response = [request responseData]; NSLog(@"%@", response); }]; [request setFailedBlock:^{ // 请求失败,获取error NSError *error = [request error]; NSLog(@"%@", [error userInfo]); }]; [request startAsynchronous]; }
但此时出现了郁闷的一幕,代码块中的request对象出现警告信息:Capturing 'request' strongly in this block is likely to lead to a retain cycle。看到这个我就知道麻烦了,而后我查了一些资料,但没有很好的解决这个问题,你们能够参考一下这里 Block的Retain Cycle的解决方法 。要是有谁知道麻烦说一下,你们相互学习。