您的位置:首页 > 理论基础 > 计算机网络

ASIHTTPRequest系列(一):同步和异步请求

2011-06-03 15:24 465 查看
ASIHTTPRequest项目地址: http://github.com/pokeb/asi-http-request/tree
,关于ASIHTTPRequest到底是什么,你可以在项目首页看到。

它提供如下功能:

提交数据到web服务器或者从web服务器获得数据;

下载数据到内存或磁盘;

采用html input相同的机制上传文件;

断点续传;

简单存取HTTP头;

上传/下载进度显示;

支持Cookie;

后台运行(iOS4.0以上支持);

对于请求和响应的GZIP支持;

支持客户端证书;

支持同步/异步请求

⋯⋯

关于它的介绍网上已经有很多了,该项目有很详细的指南文档:
How
to use ASIHTTPRequest

,也有网友翻译成中文了。本文没有完全照搬官方文档的内容,而是着重介绍了几个常见的应用,并涵盖了一些自己的理解和实际应用经验,包括:

安装、简单异步/同步请求、队列请求、上传、下载及 Cookies

注意,虽然这些技术在本文中是分开讲述的,但在实际工作中,往往是多种技术结合应用的。

此外,Http 请求往往伴随着 XML 技术的应用,实际上
ASIHTTPRequest 是可以和 SAX 异步解析结合应用的,这部分内容请参考作者另一博文《

ASIHTTPRequest和libxml结合,实现边请求边解析》。

项目下载地址: 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
这里有一份详细的入门指南。

现在,我们要做的就是,在自己的项目中使用它。

一、在项目中使用ASIHTTPRequest

1、拷贝源文件到项目中

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
目录
)

2、添加依赖库

ASIHTTPRequest 依赖于以下5个框架或库:

CFNetwork, SystemConfiguration,
MobileCoreServices, CoreGraphics 和 libz1.2.3。

依次将上述库和框架添加到 target 的 Linked Libraries
中。

二、简单的同步请求示例

新建 iOS 项目,加入必需的源文件和 Linked
Libraries。

往 MainWindow.xib 中添加一个 UIView和一个
UIButton,在 delegate 中添加相应的出口并在IB中进行连接。

编写按钮的Touch up inside代码,并连接到UIButton:

-(
IBAction
)goURL{

NSURL
*url = [
NSURL

URLWithString
:
@"http://localhost/interface/GetDept"
];

//
构造

ASIHTTPRequest
对象

ASIHTTPRequest
*request =
[
ASIHTTPRequest

requestWithURL
:url];

//
开始同步请求

[request
startSynchronous
];

NSError
*error =
[request
error
];

assert
(!error);

//
如果请求成功,返回
Response

NSString
*response =
[request
responseString
];

NSLog
(
@"%@"
,response);

}

别忘了在适当的地方导入ASIHTTPRequest:

#import

"ASIHTTPRequest.h"

分别保存IB和Xcode中所做的更改,

⌘+B

编译。

三、简单的异步请求示例

将上述代码修改为:

-(
IBAction
)goURL{

NSURL
*url = [
NSURL

URLWithString
:
@"http://localhost/interface/GetDept"
];

ASIHTTPRequest
*request = [
ASIHTTPRequest

requestWithURL
:url];

//
设定委托,委托自己实现异步请求方法

[request
setDelegate
:
self
];

//
开始异步请求

[request
startAsynchronous
];

}

并实现一系列委托方法:

//
请求结束,获取
Response
数据

- (
void
)requestFinished:(
ASIHTTPRequest
*)request

{

NSString
*responseString
= [request
responseString
];
//
对于
2
进制数据,使用:
NSData
*responseData = [request responseData];

NSLog
(
@"%@"
,responseString);

button
.
enabled
=
YES
;

}

//
请求失败,获取
error

- (
void
)requestFailed:(
ASIHTTPRequest
*)request

{

NSError
*error =
[request
error
];

NSLog
(
@"%@"
,error.
userInfo
);

button
.
enabled
=
YES
;

}

从OS X 10.6及iOS 4.0起,支持块语法,你也可以使用块语法调用ASIHTTPRequest:

-(
IBAction
)goURL{

NSURL
*url = [
NSURL

URLWithString
:
@"http://localhost/interface/GetDept"
];

__block

ASIHTTPRequest
*request = [
ASIHTTPRequest

requestWithURL
:url];

// ASIHTTPRequest
支持
iOS 4.0
的块语法,你可以把委托方法定义到块中

[request
setCompletionBlock
:^{

//
请求响应结束,返回
responseString

NSString

*responseString = [request
responseString
];
//
对于
2
进制数据,使用
NSData
返回

NSData *responseData = [request responseData];

NSLog
(
@"%@"
,responseString);

}];

[request
setFailedBlock
:^{

//
请求响应失败,返回错误信息

NSError
*error =
[request
error
];

NSLog
(
@"error:%@"
,[error
userInfo
]);

}];

[request
startAsynchronous
];

}

如果你不熟悉O-C的块语法,请参考作者另一篇博文

《块编程指南》

,或者Apple Reference Library。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: